[通过Android中的TextWatcher过滤联系人列表

问题描述 投票:0回答:1

我在ListView中显示联系人,并尝试使用EditText过滤相同的列表。但是,当我键入某些内容时,尽管键入的文本在Logcat中出现,但过滤不会发生。这是我的onCreate:

public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_invite);
    EditText filterText = findViewById(R.id.search_box);

    filterText.addTextChangedListener(filterTextWatcher);

    // The contacts from the contacts content provider is stored in this cursor
    mMatrixCursor = new MatrixCursor(new String[]{"_id", "name", "details"});

    // Adapter to set data in the listview
    mAdapter = new SimpleCursorAdapter(getBaseContext(),
            R.layout.contact_layout, null, new String[]{"name", "details"}, new int[]{R.id.tv_name, R.id.tv_details}, 0);

    // Getting reference to listview
    ListView lstContacts = findViewById(R.id.lst_contacts);

    // Setting the adapter to listview
    lstContacts.setAdapter(mAdapter);

    // Creating an AsyncTask object to retrieve and load listview with contacts
    ListViewContactsLoader listViewContactsLoader = new ListViewContactsLoader();

    // Starting the AsyncTask process to retrieve and load listview with contacts
    listViewContactsLoader.execute();

}

这是我的TextWatcher:

private TextWatcher filterTextWatcher = new TextWatcher() {

    public void afterTextChanged(Editable s) {
    }

    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before,
                              int count) {
        Log.d(TAG, "onTextChanged: " + s);
        mAdapter.getFilter().filter(s.toString());
    }

};

我在这里做错了什么?有人可以帮忙吗?

编辑:添加我的ListViewContactsLoader。

private class ListViewContactsLoader extends AsyncTask<Void, Void, Cursor> {

    @Override
    protected Cursor doInBackground(Void... params) {
        Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;

        // Querying the table ContactsContract.Contacts to retrieve all the contacts
        Cursor contactsCursor = getContentResolver().query(contactsUri,
                null, null, null,
                ContactsContract.Contacts.DISPLAY_NAME + " ASC ");

        if (contactsCursor.moveToFirst()) {
            do {
                long contactId = contactsCursor.getLong(contactsCursor
                        .getColumnIndex("_ID"));

                Uri dataUri = ContactsContract.Data.CONTENT_URI;

                // Querying the table ContactsContract.Data to retrieve individual items like
                // home phone, mobile phone etc corresponding to each contact
                Cursor dataCursor = getContentResolver().query(dataUri,
                        null,
                        ContactsContract.Data.CONTACT_ID + "=" + contactId,
                        null, null);

                String displayName = "";
                String mobilePhone = "";

                if (dataCursor.moveToFirst()) {
                    // Getting Display Name
                    displayName = dataCursor
                            .getString(dataCursor
                                    .getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
                    do {
                        // Getting Phone numbers
                        if (dataCursor
                                .getString(
                                        dataCursor
                                                .getColumnIndex("mimetype"))
                                .equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
                            switch (dataCursor.getInt(dataCursor
                                    .getColumnIndex("data2"))) {
                                case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                                    mobilePhone = dataCursor
                                            .getString(dataCursor
                                                    .getColumnIndex("data1"));
                                    break;
                            }
                        }

                    } while (dataCursor.moveToNext());

                    String details = "";

                    // Concatenating various information to single string
                    if (mobilePhone != null && !mobilePhone.equals(""))
                        details = mobilePhone + "\n";

                    // Adding id, display name, path to photo and other details to cursor
                    mMatrixCursor.addRow(new Object[]{
                            Long.toString(contactId), displayName, details});
                }

            } while (contactsCursor.moveToNext());
        }
        return mMatrixCursor;
    }

    @Override
    protected void onPostExecute(Cursor result) {
        // Setting the cursor containing contacts to listview
        mAdapter.swapCursor(result);
        Log.d(TAG, "onPostExecute: " + result);
    }
}
android filtering simplecursoradapter textwatcher
1个回答
0
投票

在EditText中添加文本后,您还需要重置ListViewadapter。

searchEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                System.out.println("Text [" + s + "] - Start [" + start + "] - Before [" + before + "] - Count [" + count + "]");
                if (count < before) {
                    listAdapter.resetData(); // MARK: Resetting adapter here
                }
                listAdapter.getFilter().filter(s);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
            }
        });

注意:resetData()是自定义方法,您必须用应用过滤器后出现的新列表数据替换列表数据。然后最后通知适配器。

© www.soinside.com 2019 - 2024. All rights reserved.