简体   繁体   中英

How i can filter ListView items ?

I'm trying to filter ListView items with SearchView .

this is my XML code. :

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:id="@android:id/list"
        android:choiceMode="none"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/searchView"
        style="@style/CodeFont">

        </ListView>

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView"
        android:layout_above="@android:id/list"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@android:id/list" />

    <SearchView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/searchView"
        android:hint="Search contact.."
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@android:id/list"

        />

and this is my java code

        ListView lv = (ListView) findViewById(android.R.id.list);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String poiName = ((TextView)(view.findViewById(android.R.id.text1))).getText().toString();
                Intent intent = new Intent(MainActivity.this, Message.class);
                intent.putExtra("name",poiName);
                startActivity(intent);
            }
        });

        Cursor =getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
        startManagingCursor(Cursor);
        String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone._ID};
        int [] to ={android.R.id.text1,android.R.id.text2};

        final SimpleCursorAdapter listadapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,Cursor,from,to);
        setListAdapter(listadapter);
        list=getListView();
        list.setChoiceMode(ListView.CHOICE_MODE_NONE);

The ListView is displaying all the contacts in the user phone. I want to make a filter with the SearchView

Assume you want to retrieve all numbers that contain whatever you have entered into your SearchView and show them in ascending order. This can be achieved with something like this:

    String query = searchView.getQuery().toString();
    Cursor cursor = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null,
            TextUtils.isEmpty(query) ? null : ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE ?",
            TextUtils.isEmpty(query) ? null : new String[] {query},
            TextUtils.isEmpty(query) ? null : ContactsContract.CommonDataKinds.Phone.NUMBER + " ASC"
    );

Do not forget to notify your adapter whenever you change text in your SearchView .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM