用户输入文本时如何使用光标更新列表视图

发布于 2024-11-17 20:46:01 字数 1796 浏览 6 评论 0原文

我完全被难住了!我已经寻找了很长时间,但找不到解决方案。我有一种感觉,这会很容易!

不管怎样,我有一个绑定到光标的列表视图,每次用户输入字母时,我希望列表视图搜索数据库并返回结果。听起来很简单,但我无法弄清楚。预先感谢您的帮助。

这是我到目前为止的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

写下不归期 2024-11-24 20:46:01

首先使 mCursor 成为成员:

private Cursor mCursor;

@Override
public void onCreate(Bundle icicle) {
  mCursor = myDb.startsWith("a");
}

然后在我的示例中,我采用 CharSequence 的第一个字母,但这可能不是您想要做的,但关键是 changeCursor();

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
  mCursor = myDb.startsWith(s.toString.substring(0,1));
  adapter.changeCursor(mCursor);
}

first make mCursor a member:

private Cursor mCursor;

@Override
public void onCreate(Bundle icicle) {
  mCursor = myDb.startsWith("a");
}

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();

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
  mCursor = myDb.startsWith(s.toString.substring(0,1));
  adapter.changeCursor(mCursor);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文