Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset listview after searchview is closed in android?

I have implemented a listview with a baseadapter that is getting filtered (only matching items are shown) when the user searches for a query, but when there is no more text in the search bar and the search action is closed, I'm not able to reset (get back all the initial listview items) the listview with all the items. I'm posting my code below. Can someone help me how to reset the listview. I have tried to call the filter() in the OnCloseListener's onClose() method by passing empty string but it doesnt help.

Activity class

    public class MainPage extends ActionBarActivity {
    MainPageAdapter mainPageAdapter;


    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_page);

        mainPageAdapter = new MainPageAdapter();
        ListView listView = (ListView)findViewById(R.id.main_list);
        listView.setAdapter(mainPageAdapter);
        listView.setTextFilterEnabled(true);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getApplicationContext(), StatePage.class);
                intent.putExtra("page number", i);
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.main_page, menu);
//        return true;
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_main_actions, menu);
//        MenuItem menuItem = menu.findItem(R.id.action_search);
        SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView)menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setQueryHint("Search State..");

        SearchView.OnQueryTextListener textListener = new SearchView.OnQueryTextListener(){

            @Override
            public boolean onQueryTextSubmit(String s) {
                mainPageAdapter.getFilter().filter(s);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                mainPageAdapter.getFilter().filter(s);
                return true;
            }
        };
        searchView.setOnQueryTextListener(textListener);

        SearchView.OnCloseListener closeListener = new SearchView.OnCloseListener(){

            @Override
            public boolean onClose() {
                return false;
            }
        };
        searchView.setOnCloseListener(closeListener);

        return super.onCreateOptionsMenu(menu);
    }

    @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();
        switch(id)
        {
            case R.id.action_search:
                return true;
            case R.id.action_overflow:
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

Adapter Class

public class MainPageAdapter extends BaseAdapter implements Filterable {

    public ArrayList<String> alist = new ArrayList<String>();
    private QueryFilter queryFilter;
    ArrayList<String> temp;
    ArrayList<String> filterStateList;

    public MainPageAdapter()
    {
        Thread thread1 = new Thread(){
            public void run(){
                alist.add("Apple");
                alist.add("Samsung");
                alist.add("HTC");
                //list goes on...
                temp = alist;
            }
        };
        thread1.start();
    }

    @Override
    public int getCount() {
        if(alist != null){
            return alist.size();
        }
        else{
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        if(alist != null){
            return alist.get(i);
        }
        else{
            return null;
        }
    }

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

    public boolean hasStableIds()
    {
        return true;
    }

    public int getViewTypeCount(){
        return 1;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if(view == null)
        {
            LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
            view = inflater.inflate(R.layout.state_list, viewGroup, false);
        }
        String state = alist.get(i);
        TextView stateTextView = (TextView)view.findViewById(R.id.state_view);
        stateTextView.setText(state);
        ImageView imageView = (ImageView)view.findViewById(R.id.flag_view);

        switch(i)
        {
            case 0:
                imageView.setImageResource(R.drawable.apple);
                break;
            case 1:
                imageView.setImageResource(R.drawable.samsung);
                break;
            case 2:
                imageView.setImageResource(R.drawable.HTC);
                break;
            //list goes on...
            default:
        }
        return view;
    }

    @Override
    public Filter getFilter() {
        if(queryFilter == null){
            queryFilter =  new QueryFilter();
        }
        return queryFilter;
    }

    private class QueryFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            FilterResults filterResults = new FilterResults();
            if(charSequence!= null && charSequence.length()> 0)
            {
                filterStateList = new ArrayList<String>();
                for(int i=0; i< temp.size(); i++)
                {
                    if((temp.get(i).toLowerCase().contains(charSequence.toString().toLowerCase())))
                    {
                        filterStateList.add(temp.get(i));
                    }
                }
                filterResults.count = filterStateList.size();
                filterResults.values = filterStateList;
            }
            else
            {
                filterResults.count = alist.size();
                filterResults.values = alist;
            }
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            alist = (ArrayList<String>) filterResults.values;
            notifyDataSetChanged();
        }
    }
}
like image 484
Prudhvi Avatar asked Jun 03 '14 03:06

Prudhvi


1 Answers

apply following in your code and check if it works,

 final MenuItem searchMenuItem = optionsMenu.findItem(R.id.search);
final SearchView searchView =
        (SearchView) searchMenuItem.getActionView();

searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean queryTextFocused) {
        if(!queryTextFocused) {
            searchMenuItem.collapseActionView();
            searchView.setQuery("", false);
        }
    }
});

or following may also work

       SearchView.OnCloseListener closeListener = new SearchView.OnCloseListener(){

        @Override
        public boolean onClose() {
            mainPageAdapter.getFilter().filter("");
            return true;
        }
    };
like image 112
Mehul Joisar Avatar answered Sep 28 '22 03:09

Mehul Joisar