How To Add Search Functionality in ListView

前端 未结 2 597
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 10:23

how do i add Search Functionality in my android app? i have a listview in which i want to add search functionality , i have the code but i dont know how do i integrate it pr

2条回答
  •  礼貌的吻别
    2020-12-20 10:58

    Since you are using android's array adapter, you can use its getFilter() method too. So you just have to change minor things in your code.

    Instead of setting array adapter like this

    list1.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, array));
    

    Create a private variable outside of the methods

    private ArrayAdapter adapter;
    

    Then initialize and set it

    adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, array);
    list1.setAdapter(adapter);
    

    Now text changed method can call MainActivity.this.adapter.getFilter().filter(cs); without any problem

提交回复
热议问题