我完全難住!我一直在尋找年齡,但找不到解決方案。我有一種感覺,它會很容易!如何在用戶輸入文字時使用光標更新列表視圖
Anway,我有一個綁定到遊標的listview,並且每次用戶輸入一個字母時,我都希望listview能夠搜索數據庫並返回結果。這聽起來很容易,但我無法弄清楚。先謝謝您的幫助。
這裏是我的代碼至今:
public class MyList extends ListActivity {
public static final String KEY_ROWID = "headwords._id";
public static final String KEY_WORDS = "headwords.words";
private ListAdapter adapter;
private DbManager myDb;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.item_list);
myDb = new DbManager(this);
myDb.open();
Cursor mCursor = myDb.startsWith("a");
startManagingCursor(mCursor);
try {
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, mCursor,
new String[] { KEY_WORDS, KEY_ROWID },
new int[] { android.R.id.text1, android.R.id.text2 });
setListAdapter(adapter);
} catch (Exception e) {
Log.i("adapter error Here!", ">>>>>>>>>>> Error!" + e.toString());
}
EditText myET = (EditText) findViewById(R.id.search_text);
myET.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//code which would change listview to only show item that match what is being typed in
}
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
});
}
使你的mCursor最終和調用mCursor.requery()onTextChanged; – Gopal
重新查詢將不起作用,因爲它不會像以前一樣查詢。 –