0

I am trying to use autocompletetextview and referred to some tutorials.The following is the code I'hv got so far.

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);
            }
              });

In the DatabaseHelper class I used the following implementation

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;
     }

But after typing single letter in the autocompletetextview,it is getting disabled and next activity(Shops activity) is starting without any displaying of sugestions.I am unable to enter any input.It is throwing the following errors

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)

Please tell me where I'm going wrong.Thanks in advance.

user3794853
  • 55
  • 1
  • 8
  • see my answer here: http://stackoverflow.com/questions/19858843/how-to-dynamically-add-suggestions-to-autocompletetextview-with-preserving-chara – pskink Jul 11 '14 at 20:55

1 Answers1

0

If you are accessing data from Cursor object than you must have to position the Cursor object.

Actually you have to position Cursor to the first row before you try to access data from it.

After you execute query, you must call cur.moveToFirst()...

Try this

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();

Second thing

Whenever you are dealing with Cursors, ALWAYS check for null and check for moveToFirst() without fail.

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

Place logs appropriately to see whether it is returning null or an empty cursor. According to that check your query.

Zare Ahmer
  • 808
  • 5
  • 8
  • The cursor is not empty.It's returning 3 rows.The same error is persisting.Is it correct to use textChangedListener for autoCompleteTextView? – user3794853 Jul 12 '14 at 02:28