我有一个EditText
,用于获取用户输入。用户输入某些数据后,与EditText
关联的文本更改侦听器需要刷新光标,并试图更新在ListView
中显示的结果,并将其放在下面。
一切都很好。但是,只要发生搜索查询的任何更改,则结果光标和ListView
更新需要一些时间,例如 n 秒。在 n second的此跨度中,UI停止(停止/悬挂您可以打电话给任何呼叫),并且在可用的光标可用并且整个ListView
都填充了。
当我试图将光标的更新放在其他线程中时,它不允许在UI中反映出光标,因为UI-Thread不允许其他线程在操作中命令。任何UI活动(例如列表更新)都必须通过MainActivity
类中的runOnUiThread
实现。
请建议我允许用户修改EditText的方法以及更新的光标刷新listView发生而不会影响前者。
基本上,您正在尝试错误的方法。当我们希望列表的数据直接从SQLite数据库查询中采购时,我们可以使用CursorAdapter。
创建一个适配器
public class MyCursorAdapter extends CursorAdapter {
// Default constructor
public MyCursorAdapter(Context context, Cursor cursor, int flags) {
...
}
public void bindView(View view, Context context, Cursor cursor) {
...
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
...
return null;
}
}
Get Values from database
Cursor todoCursor = db.rawQuery("SELECT * FROM todo_items", null);
现在,我们可以在活动中使用CursorAdapter将项目数组显示到listView:
// Find ListView to populate
ListView lvItems = (ListView) findViewById(R.id.lvItems);
// Setup cursor adapter using cursor from last step
TodoCursorAdapter todoAdapter = new TodoCursorAdapter(this, todoCursor);
// Attach cursor adapter to the ListView
lvItems.setAdapter(todoAdapter);
This will then trigger the CursorAdapter iterating through the result set and populating the list. We can change the cursor to update the adapter at any time with:
// Switch to new cursor and update contents of ListView
todoAdapter.changeCursor(newCursor);