问题
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
回答1:
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);
回答2:
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);
回答3:
try this :
acTextView.setThreshold(1);
acTextView.addTextChangedListener(this);
acTextView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, newone));
来源:https://stackoverflow.com/questions/14626068/returning-string-array-and-use-it-on-autocompletetextview