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.