0

I am a novice! user, I am trying to filter my data using Search View. Filtering seems to work, but when I delete the text in search view to type another, I have only the found item and not my initial list.

More analytically I use the following in the adapter:

public void filterData(String query){

    query = query.toLowerCase();

    if(query.isEmpty()){
        expListTitle .addAll(InitialList);

    }
    else {

        expListTitle.clear();
        for(String titles: NewexpListTitle)
        {
            if(titles.toLowerCase().contains(query))
            {
                expListTitle.add(titles);
            }
        }

    }

    notifyDataSetChanged();

}

and in the constructor of adapter I do the following:

public CustomExpandableListAdapter(Context context, List<String> expListTitle, LinkedHashMap<String,ArrayList<Business>> expListDetail)
{
    this.context = context;
    this.expListTitle = expListTitle;
    this.expListDetail = expListDetail;

    this.NewexpListTitle= new ArrayList<String>();
    this.NewexpListTitle.addAll(expListTitle);

    this.InitialList = new ArrayList<String>();
    this.InitialList.addAll(expListTitle);
}

And on my class I gave the following which call FilterData method

@Override
public boolean onQueryTextChange(String query) {
    listAdapter = new CustomExpandableListAdapter(getApplicationContext(), FinalListTitle, FinalMap);

    listAdapter.filterData(query);

    expandableListView.setAdapter(listAdapter);
    return false;
}

I have above the setAdapter and initialization because data are coming from JSON object using volley

It seems that when I am clearing the SearchView the list InitialList is cleared as well and its left with the found element . How to keep my initial list in the meanwhile?

Thank you

Ankita Shah
  • 1,866
  • 16
  • 31

1 Answers1

0

Try this,

@Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        str=charSequence.toString();
        if(charSequence.length() >0){
            try {

                vRow.clear();
                for (RowItem r : vvRow){
                    ss= r.title.toLowerCase().toString();
                    if (ss.contains(str.toLowerCase())){
                        vRow.add(r);
                    }
                }
                adapter.notifyDataSetChanged();
            }catch (Exception e){
                Toast.makeText(getActivity(),e.getMessage(),Toast.LENGTH_SHORT).show();
            }
        }else{
            searchedt.setCompoundDrawablesWithIntrinsicBounds(0,0,0,0);
            try {
                vRow.clear();
                for (RowItem r : vvRow){
                    vRow.add(r);
                }
                adapter.notifyDataSetChanged();
            }catch (Exception e){
                Toast.makeText(getActivity(),e.getMessage(),Toast.LENGTH_SHORT).show();
            }
        }

    }
Divyesh Patel
  • 2,576
  • 2
  • 15
  • 30