Search code examples
androidandroid-listviewsearchview

I am not getting correct output of searching an item on listview


Adapter setup in onCreate

  myAdapter = new MyAdapter(emp_list_activity.this,R.layout.checkable_list_layout,listview_array);
    search_view_j.setOnQueryTextListener(this);
    employee_list_View.setAdapter(myAdapter);

  search_view_j.setOnQueryTextListener(this);

@Override
public boolean onQueryTextSubmit(String query) {
    return false;
}

@Override
public boolean onQueryTextChange(String newText) {
    myAdapter.getFilter().filter(newText);
    return false;
}



 private class MyAdapter extends ArrayAdapter {

    public MyAdapter(Context context, int resource, ArrayList<String> objects) {
        super(context, resource, objects);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View v = getLayoutInflater().inflate(R.layout.checkable_list_layout,null);


        return v;
    }

i am using this custom listview and adding a searview on the listview but i am not getting output but when i use simple listview without custom i am getting the perfect output ,can you please check the code and provide me solution that where is the problem in my code

custom layout xml file checkablelistlayout.xml

    <RelativeLayout  
       xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout01"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:textSize="18dp"
    android:layout_marginLeft="10dp"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:id="@+id/list_tv"/>

</RelativeLayout>

i am searching for items but it shows only one item that is already on the top if not matches shows blank. so please tell me what is the problem


Solution

  • Look at this code

    ** On your recyclerview activity ** 1) create edit text 2)find out id 3) add addTextChangeListener

    search_txt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
    
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                filter(s.toString());
            }
        });
    

    4) add method

    private void filter(String txt)
    {
        ArrayList<ModelClass> temList=new ArrayList<>();
    
        for(ModelClass s:list)
        {
            if(s.getName().contains(txt))
            {
                temList.add(s);
            }
        }
    
        adapter.filterList(temList); //call this method which is written in adapter class
    }
    

    5) In adapter class write method below getItemCount() (for refrence)

    public void filterList(ArrayList<ModelClass> fliterNm)
    {
        this.data=fliterNm;
        notifyDataSetChanged();
    }
    

    I hope this answer will help you..