返回字符串数组,并使用它AutoCompleteTextview [英] Returning string array and use it on AutoCompleteTextview
问题描述
我正在开发中,我检索SQLite数据库表中的一些数据块(所有的村容村名)Android应用程序。我通过字符串数组返回此。 code是
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;
}
和,在我的Android应用我通过这个数组AutoCompleteTextview如下:
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);
不过,code没有任何效果,这意味着,当我点击AutocompleteTextview,它不会显示任何villagenames。是我的方法是正确的?如果没有,那么帮助我做到这一点。
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
推荐答案
首先,在code中的问题是
First, the problem in your code is
String[] village=new String[2000];// assigning large size
String newone[] = new String[2000];
原因是假设,如果只有1000记录在数据库present。这两个字符串数组包含高达剩余的1000结果和空值。您可以通过断点检查。自动完成的TextView不支持这种格式。
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.
所以,你应该在你的code一些变化
So you should make some changes in your code as
int count;
count=vadapter.getCount();
String[] village=new String[count];
village=vadapter.getAllVillage();
然后就可以直接使用setAdapter村为
Then you can directly use village in setAdapter as
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,village);
这篇关于返回字符串数组,并使用它AutoCompleteTextview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!