【发布时间】:2014-06-10 04:18:04
【问题描述】:
我有一个词典数据库,包含超过 300,000 个单词,使用 cursoradapter 填充 ListView。 ListView过滤一个词时,软件有延迟,有什么方法可以立即过滤?
这是Main的代码
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db=new WordDbAdapter(this);
db.openDatabase();
Cursor cursor=db.getAllWord();
adapter=new SimpleCursorAdapter(this, R.layout.listview_item, cursor,new String[] {"word"},new int[] {R.id.title}, 0);
lv=(ListView) findViewById(R.id.listView1);
lv.setAdapter(adapter);
filter=(EditText) findViewById(R.id.editText1);
filter.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
adapter.getFilter().filter(cs.toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
这是数据库的代码:
public Cursor getAllWord()
{
Cursor cursor = null;
try {
String sql="select rowid _id,word from anh_viet";
cursor=mdb.rawQuery(sql, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG).show();
}
return cursor;
}
public Cursor filterWord(String key)
{
Cursor cursor = null;
try {
String sql="select rowid _id,word from anh_viet where word like '"+key+"%'";
cursor=mdb.rawQuery(sql, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG).show();
}
return cursor;
}
【问题讨论】:
标签: android listview dictionary android-cursoradapter