AutoCompleteTextView无法正常工作

时间:2014-07-11 20:52:42

标签: android android-arrayadapter autocompletetextview

我正在尝试使用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);
            }
              });

在DatabaseHelper类中,我使用了以下实现

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我无法输入任何输入。它会抛出以下错误

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)

请告诉我哪里出错了。谢谢。

1 个答案:

答案 0 :(得分:0)

如果要从Cursor对象访问数据,则必须定位Cursor对象。

实际上,在尝试从中访问数据之前,必须将Cursor定位到第一行。

执行查询后,您必须致电cur.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();

第二件事

每当你处理游标时,总是检查null并检查moveToFirst()是否正确。

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

适当地放置日志以查看它是返回null还是空光标。根据检查你的查询。