How To Add Search Functionality in ListView

前端 未结 2 602
被撕碎了的回忆
被撕碎了的回忆 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

    0 讨论(0)
  • 2020-12-20 11:12

    Try the below

    EditText inputSearch; 
    ArrayAdapter<String> adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    inputSearch = (EditText) findViewById(R.id.inputSearch); // initialize edittext 
    list1 = (ListView) findViewById(R.id.ListView01);
    adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array));
    list1.setAdapter(adapter);
    inputSearch.addTextChangedListener(new TextWatcher() {
    
    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
        // When user changed the Text
        MainActivity.this.adapter.getFilter().filter(cs);   
    }
    
    @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                          
    }
    });
    list1.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
         if (position == 1)
        {
          Intent myIntent = new Intent(getApplicationContext(),Baba.class);
          startActivity(myIntent);
        }
      }
    });
    }
    

    Initialize editText inputSearch in onCreate.

    inputSearch = (EditText) findViewById(R.id.inputSearch); // initialize edittext 
    

    Initialize adapter

    adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array));
    

    Set the adapter op listview

    list1.setAdapter(adapter);
    

    This is where the search happens

     @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
        // When user changed the Text
        MainActivity.this.adapter.getFilter().filter(cs);   // filter based on input
    }
    
    0 讨论(0)
提交回复
热议问题