0

I'm developing an android application in which I retrieve some piece of data ( all the village village names) from SQLite database table. I returned this via String Array. Code is

public String[] getAllVillage()
 {
     String[] villagelist=new String[2000];
     int i=0;
     Cursor c=sqLiteDatabase.rawQuery("SELECT * FROM " + MYDATABASE_TABLE2 ,null);
     if (c.moveToFirst())
     {
         do
         {
             villagelist[i]=c.getString(1);
             i++;
         }while(c.moveToNext());
     }
     c.close();
    return villagelist;
 }

and, In my android application I passed this array into AutoCompleteTextview as following:

private SQLiteAdapterv vadapter;
String[] village=new String[2000];
String newone[] = new String[2000];
village=vadapter.getAllVillage();
 for(int h=0;h<2000;h++)
 {
newone[h]=village[h];
 }
   ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,newone);
   final AutoCompleteTextView acTextView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);

   acTextView.setThreshold(0);
   acTextView.setAdapter(adapter);
   acTextView.addTextChangedListener(this);

But the code has no effect which means, when I click the AutocompleteTextview, it won't show any villagenames. Is my method was correct? If not then help me to do this

Linga
  • 10,379
  • 10
  • 52
  • 104
  • did you set breakpoints and look through your code? DO you know if your query is filling up `villagelist` ? –  Jan 31 '13 at 12:48
  • No I didn't check with breakpoints. Is my procedure is correct? I mean returning string array and implementing it – Linga Jan 31 '13 at 12:49
  • check [this](http://www.mysamplecode.com/2011/10/android-autocompletetextview.html) out. They seem to be doing what you want. But I would start by placing breakpoints to make sure you are getting your data first. –  Jan 31 '13 at 12:56
  • I've checked. No problem in query. Both village and newone contains result – Linga Jan 31 '13 at 13:16

3 Answers3

2

First, the problem in your code is

String[] village=new String[2000];// assigning large size
String newone[] = new String[2000];

The reason is assume that if only 1000 records are present in your database. these two string arrays contains 1000 result and null values upto the remaining. You can check by breakpoint. AutoComplete Textview doesn't support such format.

So you should make some changes in your code as

int count;
count=vadapter.getCount();
String[] village=new String[count];
village=vadapter.getAllVillage();

Then you can directly use village in setAdapter as

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,village);
1

ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,newone);

replace with this;

ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,village);

QuokMoon
  • 4,387
  • 4
  • 26
  • 50
  • did you break through it? are there any elements in either `village` or `newone`? If they contain your villages, then you know the issue is in your code, if not, then you need to fix your query. This is important to know –  Jan 31 '13 at 13:11
  • for(int h=0;h – QuokMoon Jan 31 '13 at 13:13
  • I've checked. No problem in query – Linga Jan 31 '13 at 13:15
0

try this :

acTextView.setThreshold(1);

acTextView.addTextChangedListener(this);

acTextView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, newone));
Anand
  • 2,875
  • 1
  • 18
  • 16