基本上,你正在尝试错误的方法。当我们希望列表的数据直接来自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);
我已经使用一个CursorAdaptor并在bindView方法,我已经完成了所有必须完成的工作。但是,在为返回400行的给定查询获取结果期间,需要一定的时间来处理其他UI功能处于挂起状态。 –
你用过textChangeListner吗? –
使用内容obsever查看反映更改。 400条记录无关紧要。你可以使用限制和分页 –