用户输入文本时如何使用光标更新列表视图
我完全被难住了!我已经寻找了很长时间,但找不到解决方案。我有一种感觉,这会很容易!
不管怎样,我有一个绑定到光标的列表视图,每次用户输入字母时,我希望列表视图搜索数据库并返回结果。听起来很简单,但我无法弄清楚。预先感谢您的帮助。
这是我到目前为止的代码:
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) {
}
});
}
I'm totally stumped! I've been searching for ages, but can't find a solution. I have a feeling it is going to be really easy though!
Anway, I have a listview which is bound to a cursor, and every time the user enters a letter i want the listview to search the database and return the results. It sounds pretty easy, but I can't figure it out. Thanks in advance for the help.
Here is the code I have so far:
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) {
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先使 mCursor 成为成员:
然后在我的示例中,我采用 CharSequence 的第一个字母,但这可能不是您想要做的,但关键是 changeCursor();
first make mCursor a member:
Then in my example I am taking the first letter of the CharSequence but this might not be what you want to do but the key is the changeCursor();