Search code examples
androidsearchviewcustom-adapterandroid-searchmanager

ListView not updating after Search using SearchView in android


I am trying to develop an app which will have a SearchView.I have successfully implemented the logic for filtering the results and updating in ListView.But When i am cancelling or closing the search ,the data on the ListView is not reverted back to it's old list.I have been trying all the possible ways and stuck here for long time.Please help me out solving this problem. Here's the code I have written:

MainActivity.java

package com.example.arushi.searchviewcustom;

  import android.os.Bundle;
      import android.support.v7.app.AppCompatActivity;
      import android.support.v7.widget.Toolbar;
    import android.text.TextUtils;
    import android.view.Menu;
     import android.view.MenuItem;
   import android.view.View;
     import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
   import android.widget.ListView;
    import android.widget.Toast;

    import java.util.ArrayList;

    public class MainActivity extends AppCompatActivity {
ListView lv_listname;
ArrayList<Beanclass> al_names,al_names2;
ArrayList<String> altype;
Adap ad_names;
ArrayAdapter<String> ad;
com.example.arushi.searchviewcustom.MaterialSearchView searchview;
Toolbar toolbar;
android.widget.Filter filter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv_listname = (ListView) findViewById(R.id.listView);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //  toolbar.setNavigationIcon(R.drawable.back_arrow_white);

    searchview = (com.example.arushi.searchviewcustom.MaterialSearchView) findViewById(R.id.searchview);

    al_names = new ArrayList<>();
   /* altype=new ArrayList<>();

    altype.add("Rushi");
    altype.add("ayyappa");
    altype.add("kiran");
    altype.add("priya");
    altype.add("swathi");
    altype.add("pen");
    altype.add("bat");
    altype.add("cat");
    ad = new ArrayAdapter<String>   
       (MainActivity.this,android.R.layout.simple_list_item_1,altype);
    lv_listname.setAdapter(ad);
    filter=ad.getFilter();*/
    //         ad.notifyDataSetChanged();
    al_names.add(new Beanclass("Rushi", 22));
    al_names.add(new Beanclass("Swathi", 22));
    al_names.add(new Beanclass("Priya", 22));
    al_names.add(new Beanclass("Kiran", 22));
    al_names.add(new Beanclass("Laskshmi", 22));
    al_names.add(new Beanclass("Hammad", 22));
    al_names.add(new Beanclass("Anil", 22));
    al_names.add(new Beanclass("Anand", 22));
    al_names2=al_names;
    ad_names = new Adap(MainActivity.this,al_names);
    lv_listname.setAdapter(ad_names);

    lv_listname.setTextFilterEnabled(false);
 //   filter = ad_names.getFilter();
    //ad_names.notifyDataSetChanged();
    /*setupSearchView();*/

         searchview.setOnSearchViewListener(new       


           com.example.arushi.searchviewcustom.MaterialSearchView.SearchViewListener() {
        @Override
        public void onSearchViewShown() {
            searchview.setVisibility(View.VISIBLE);
            Toast.makeText(getApplicationContext(), "opened ", Toast.LENGTH_LONG).show();
            toolbar.setVisibility(View.GONE);
        }

        @Override
        public void onSearchViewClosed() {
        //  lv_listname.clearTextFilter();

            Toast.makeText(getApplicationContext(), "closed ", Toast.LENGTH_LONG).show();
            toolbar.setVisibility(View.VISIBLE);

        }
    });


    searchview.setOnQueryTextListener(new com.example.arushi.searchviewcustom.MaterialSearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            if (TextUtils.isEmpty(newText)) {
                lv_listname.clearTextFilter();
            } else {
                 // lv_listname.setFilterText(newText);
                ad_names.getFilter().filter(newText);
                //filter.filter(newText);
            }
            return true;
        }
    });
    lv_listname.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(), "you clicked on " + al_names.get(position), Toast.LENGTH_LONG).show();
        }
    });
    // searchview.setSubmitButtonEnabled(true);
    searchview.setHint("Search Containers");
}

private void setupSearchView() {
    //  searchview.setIconifiedByDefault(false);

    //   searchview.seti
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    MenuItem item = menu.findItem(R.id.searchicon);
    searchview.setMenuItem(item);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.searchicon) {
        searchview.setVisibility(View.VISIBLE);

    }



    return super.onOptionsItemSelected(item);
}



  }

Here's my AdapterClass

package com.example.arushi.searchviewcustom;

        import android.content.Context;
        import android.text.TextUtils;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.BaseAdapter;
        import android.widget.Filter;
        import android.widget.Filterable;
        import android.widget.TextView;
        import java.util.ArrayList;
        import java.util.List;
       class Adap extends BaseAdapter implements Filterable {
         public ArrayList<Beanclass> mData;
         public ArrayList<Beanclass> mFilteredData ;
        Context ctx;

        public Adap(Context ctx,ArrayList<Beanclass> al_names) {
        mData=al_names;
   mFilteredData=al_names;
    this.ctx=ctx;
}


@Override
public int getCount() {
    return  mFilteredData.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   // LayoutInflater lf =;
    convertView = LayoutInflater.from(ctx).inflate(R.layout.pattern, parent, false);
    TextView tv = (TextView) convertView.findViewById(R.id.textView);
    TextView tv2 = (TextView) convertView.findViewById(R.id.textView2);
    tv.setText(mFilteredData.get(position).getName());
    tv2.setText("age:" + mFilteredData.get(position).getAge());

    return convertView;
}

@Override
public Filter getFilter() {

  /*  if (mContactsFilter == null)
        mContactsFilter = new Filterer(mData);

    return mContactsFilter;
}*/
    return new Filter() {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults oReturn = new FilterResults();
            ArrayList<Beanclass> results = new ArrayList<Beanclass>();
            if (TextUtils.isEmpty(constraint))
            //   orig = al_namesfilter;
            {
                oReturn.count = mData.size();
                oReturn.values = mData;
            } else {
                List<Beanclass> resultList = new ArrayList<>();
                //    if (orig != null && orig.size() > 0) {

                for (final Beanclass g : mData) {
                    if ((g.getName().toLowerCase()
                            .contains(constraint.toString())))
                        //   results.add(g);
                        resultList.add(g);
                }
                //  }
                oReturn.count = resultList.size();
                oReturn.values = resultList;
            }
            return oReturn;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
           /* al_namesfilter = (ArrayList<Beanclass>) results.values;
            notifyDataSetChanged();*/
            if (results.count == 0) {
                notifyDataSetInvalidated();
            } else {
                mFilteredData = (ArrayList<Beanclass>) results.values;
                notifyDataSetChanged();
            }
        }
    };

}
         }

Please help me out where am I doing wrong?


Solution

  • this below code solved my issue.
    
            @Override
            public boolean onQueryTextChange(String newText) {
                if (TextUtils.isEmpty(newText)) {
                    lv_listname.clearTextFilter();
                    ad_names.getFilter().filter("");
                } else {
                   ad_names.getFilter().filter(newText);
                  //  ad_names.notifyDataSetChanged();
                  //  lv_listname.setFilterText(newText);
                  //  searchview.dismissSuggestions();
                }
                return true;
            }