Android 如何在用户输入文本时使用光标更新listview

Android 如何在用户输入文本时使用光标更新listview,android,listview,cursor,adapter,Android,Listview,Cursor,Adapter,我完全被难住了!我已经找了很久了,但找不到解决办法。我有一种感觉,这将是非常容易虽然 另外,我有一个绑定到光标的listview,每次用户输入一个字母时,我都希望listview搜索数据库并返回结果。听起来很容易,但我想不出来。提前谢谢你的帮助 以下是我目前掌握的代码: public class MyList extends ListActivity { public static final String KEY_ROWID = "headwords._id"; publi

我完全被难住了!我已经找了很久了,但找不到解决办法。我有一种感觉,这将是非常容易虽然

另外,我有一个绑定到光标的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成为会员:

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

使您的mCursor成为最终版本,并在ContextChanged中调用mCursor.requery;重新查询将不起作用,因为它将与以前的查询不同。感谢您花时间回答问题并如此迅速!这段代码看起来很棒,但是eclipse说类型ListAdapter的方法ChangeCursor是未定义的。你知道为什么吗?你的适配器成员应该被转换成SimpleCorsAdapter。所以改变私有列表适配器;到专用SimpleCorsorAdapter适配器;哼!我想该睡觉了。谢谢你的帮助,我现在工作得很好,即使有点慢。顺便说一句,Hat rulz先生!别忘了将此标记为答案。这会增加你的声誉!