开发者

How to update listview using cursor while user enters text

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.s开发者_运维技巧imple_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) {    
            }

        });

    }


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜