我正在尝试从 SQLite 表格填充数据,但自动填充功能似乎没有显示任何建议。
当我检查ArrayList时,它具有所需的数据。我在这里错过了什么或做错了什么?
AutoCompleteTextView代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mybusapp.BusRoutes" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/auto_complete"
android:textAppearance="?android:attr/textAppearanceMedium" />
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:ems="10" />
</RelativeLayout>
**返回数据的函数**
public ArrayList<String> getByRoutes1()
{
String selectQuery = "SELECT * FROM " + TABLE_BUSROUTE + " ORDER BY 1 DESC";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
ArrayList<String> items = new ArrayList<String>();
try {
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()){
items.add(cursor.getString(1));
cursor.moveToNext();
}
}
} finally {
try { cursor.close(); } catch (Exception ignore) {}
}
// return route list
return items;
}
活动类代码
private AutoCompleteTextView actv;
DBHelper dbHandler = new DBHelper(this, null, null, 1);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bus_routes);
ArrayList<String> values = dbHandler.getByRoutes1(); // The values populate here
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, values);
actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
actv.setThreshold(1);
actv.setAdapter(adapter);
如果我使用下面的数组,它似乎填充正确。我只是将“值”替换为“COUNTRIES”。
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
EDIT1
getByRoutes1函数返回的数据如下:
[31,46,null,null,S64,114,121,64C,164,38A]
我认为 null 可能导致问题
EDIT2
问题在于返回空数据。我将函数名称更改为更具可读性。
public ArrayList<String> getByRoutesFilter()
{
String selectQuery = "SELECT * FROM " + TABLE_BUSROUTE + " WHERE routeno is not null ORDER BY 1 DESC";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
ArrayList<String> items = new ArrayList<String>();
try {
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()){
items.add(cursor.getString(1));
cursor.moveToNext();
}
}
} finally {
try { cursor.close(); } catch (Exception ignore) {}
}
// return route list
return items;
}
答案 0 :(得分:0)
尝试添加此代码,为我工作
edt=(EditText)findViewById(R.id.EditText01);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < sightstitle.length; i++) {
RowItem item = new RowItem(sightimages[i], sightstitle[i],sightimagearow[i]);
rowItems.add(item);
}
listView = (ListView) findViewById(R.id.list);
final CustomBaseAdapter adapter = new CustomBaseAdapter(this, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
edt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
adapter.filter(s.toString().trim());
}
});
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast toast = Toast.makeText(getApplicationContext(),
"Item " + (position + 1) + ": " + rowItems.get(position),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}