2014-07-11 5 views
0

autocompletetextview를 사용하려고하고 일부 자습서를 참조했습니다. 다음 코드는 지금까지 얻은 코드입니다. DatabaseHelper 클래스에서 AutoCompleteTextView가 제대로 작동하지 않습니다.

final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocompleteProduct); 
     db=new DatabaseHelper(this); 

     String[] products=db.getAllProducts(); 


      adapter = new ArrayAdapter<String>(this, R.layout.list_item, products); 
     textView.setThreshold(1); 
     textView.setAdapter(adapter); 

     textView.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       textView.showDropDown(); 

      } 
     }); 

     textView.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void afterTextChanged(final Editable editable) { 

      } 


      @Override 
      public void beforeTextChanged(final CharSequence string, 
        final int start, final int count, final int after) { 
      } 


      @Override 
      public void onTextChanged(final CharSequence string, final int start, 
        final int before, final int count) { 
       System.out.println("in ontextchanged ps"); 
       Intent i=new Intent(ProductsSearch.this,Shops.class); 
       startActivity(i); 
      } 
       }); 

나는

public String[] getAllProducts() 
{ 
    String[] str=null; 
    sdb=this.getReadableDatabase(); 
    c=null; 
    try{ 
    c=sdb.rawQuery("select ProductName from Products",null); 
    System.out.println("c count="+c.getCount()); 
    c.moveToFirst(); 
     if(c.getCount() >0) 
    { 
     str = new String[c.getCount()]; 
     int i = 0; 

     do 
     { 
       str[i] = c.getString(c.getColumnIndex("ProductName")); 
       i++; 
      }while (c.moveToNext()); 

     c.close(); 
     sdb.close(); 
    } 

    }catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
     return str; 
    } 

는하지만, 비활성화 및 다음 활동 (상점 활동을) 받고있는 AutoCompleteTextView에에 단일 문자를 입력 한 후 어떤 sugestions의 표시하지 않고 시작하고 다음 구현을 사용 .I는 input.It를 입력 할 수없는 나는 내가 미리 wrong.Thanks 갈거야 어디

07-11 16:48:29.873: W/System.err(2045): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 
07-11 16:48:29.883: W/System.err(2045):  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426) 
07-11 16:48:29.883: W/System.err(2045):  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 
07-11 16:48:29.883: W/System.err(2045):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50) 
07-11 16:48:29.883: W/System.err(2045):  at helper.DatabaseHelper.getShopNames(DatabaseHelper.java:454) 
07-11 16:48:29.953: W/System.err(2045):  at com.example.cognizantshopping.Shops.onCreate(Shops.java:30) 

가 알려주세요 다음과 같은 오류를 던지고있다.

+0

내 대답보기 http://stackoverflow.com/questions/19858843/how-to-dynamically-add-suggestions-to-autocompletetextview-with-preserving-chara – pskink

답변

0

커서 개체에서 데이터에 액세스하는 경우보다 커서 개체를 배치해야합니다.

실제로 커서에서 데이터에 액세스하기 전에 먼저 커서를 첫 번째 행에 배치해야합니다. 당신이 커서를 처리 할 때마다

당신이 쿼리를 실행 한 후에는 cur.moveToFirst()를 호출해야합니다 ...

항상 널 (null)를 확인하고 moveToFirst을 확인,이

String getPost = "SELECT * FROM " + TABLE_POST + ";"; 
    Cursor result = getReadableDatabase().rawQuery(getPost, null); 
    List posts = new ArrayList();{ 
    Log.d("count", result.getCount() + " post rows"); 

    if (result .moveToFirst()) { 
     do { 
      Post post = new Post(); 
      post.setPostId(result.getInt(0)); 
      posts.add(post); 
       .... 
     } while (result .moveToNext()); 
    } 
    result .close(); 

두 번째 것은

시도() 틀림없이.

if(cursor != null && cursor.moveToFirst()){ 
    num = cursor.getString(cursor.getColumnIndex("ContactNumber")); 
    cursor.close(); 
} 

로그를 올바르게 두어 null 또는 빈 커서가 반환되는지 확인하십시오. 에 따르면 귀하의 쿼리를 확인하십시오.

+0

커서가 비어 있지 않습니다. 3 행을 반환 중입니다. . 동일한 오류가 지속됩니다. autoCompleteTextView에 textChangedListener를 사용하는 것이 맞습니까? – user3794853

관련 문제