Search code examples
androidlistviewandroid-fragmentsandroid-listview

Implement Search in ListView inside Fragment


I am trying to implement search functionality in a listview built into a fragment. The listview is working fine, but when I type on the edittext to search, it disappears.

My code:

public class DrinksFragment extends Fragment {

    private View rootView;
    private ArrayAdapter<DrinksList> adapter;
    private List<DrinksList> drinks;
    private ListView lv;
    ArrayList<DrinksList> mAllData;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.activity_drinks_fragment, container, false);
        populateDrinksList();
        doSearch();
        return rootView;
    }

    private void doSearch() {
        final EditText et = (EditText)rootView.findViewById(R.id.searchListDrinks);
        et.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) {
                String text = et.getText().toString().toLowerCase(Locale.getDefault());
            adapter.filter(text);
            }
        });
    }



    private void populateDrinksList() {
        drinks = new ArrayList<DrinksList>();
        drinks.add(new DrinksList(R.drawable.coca, "Coca Cola", 2.50));
        drinks.add(new DrinksList(R.drawable.cocalight, "Coca Cola Light", 2.50));
        drinks.add(new DrinksList(R.drawable.cocazero, "Coca Cola Zero", 2.50));
        drinks.add(new DrinksList(R.drawable.orange, "Fanta Orange", 2.50));
        drinks.add(new DrinksList(R.drawable.lemon, "Fanta Lemon", 2.50));
        drinks.add(new DrinksList(R.drawable.ble, "Fanta Blue", 2.50));
        drinks.add(new DrinksList(R.drawable.sprite, "Sprite", 2.50));
        drinks.add(new DrinksList(R.drawable.soda, "Soda Water", 2.50));
        drinks.add(new DrinksList(R.drawable.tonic, "Tonic Water", 2.50));
        drinks.add(new DrinksList(R.drawable.ioli, "Sparkling Water Ioli", 2.50));
        drinks.add(new DrinksList(R.drawable.perrier, "Sparkling Water Perrier", 2.50));
        drinks.add(new DrinksList(R.drawable.nero, "Still Water", 2.00));
        drinks.add(new DrinksList(R.drawable.redbull, "Red Bull", 4.00));
        drinks.add(new DrinksList(R.drawable.zelita, "Zelita", 2.50));
        lv = (ListView)rootView.findViewById(R.id.drinksListView);
        adapter = new MyCustomDrinksListAdapter(getActivity().getApplicationContext(), R.layout.list_item, drinks);
        lv.setAdapter(adapter);
    }

}public class DrinksFragment extends Fragment {

    private View rootView;
    private ArrayAdapter<DrinksList> adapter;
    private List<DrinksList> drinks;
    private ListView lv;
    ArrayList<DrinksList> mAllData;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.activity_drinks_fragment, container, false);
        populateDrinksList();
        doSearch();
        return rootView;
    }

    private void doSearch() {
        final EditText et = (EditText)rootView.findViewById(R.id.searchListDrinks);
        et.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) {
                DrinksFragment.this.adapter.getFilter().filter(s);
            }

            @Override
            public void afterTextChanged(Editable s) {
                
            }
        });
    }



    private void populateDrinksList() {
        drinks = new ArrayList<DrinksList>();
        drinks.add(new DrinksList(R.drawable.coca, "Coca Cola", 2.50));
        drinks.add(new DrinksList(R.drawable.cocalight, "Coca Cola Light", 2.50));
        drinks.add(new DrinksList(R.drawable.cocazero, "Coca Cola Zero", 2.50));
        drinks.add(new DrinksList(R.drawable.orange, "Fanta Orange", 2.50));
        drinks.add(new DrinksList(R.drawable.lemon, "Fanta Lemon", 2.50));
        drinks.add(new DrinksList(R.drawable.ble, "Fanta Blue", 2.50));
        drinks.add(new DrinksList(R.drawable.sprite, "Sprite", 2.50));
        drinks.add(new DrinksList(R.drawable.soda, "Soda Water", 2.50));
        drinks.add(new DrinksList(R.drawable.tonic, "Tonic Water", 2.50));
        drinks.add(new DrinksList(R.drawable.ioli, "Sparkling Water Ioli", 2.50));
        drinks.add(new DrinksList(R.drawable.perrier, "Sparkling Water Perrier", 2.50));
        drinks.add(new DrinksList(R.drawable.nero, "Still Water", 2.00));
        drinks.add(new DrinksList(R.drawable.redbull, "Red Bull", 4.00));
        drinks.add(new DrinksList(R.drawable.zelita, "Zelita", 2.50));
        lv = (ListView)rootView.findViewById(R.id.drinksListView);
        adapter = new MyCustomDrinksListAdapter(getActivity().getApplicationContext(), R.layout.list_item, drinks);
        lv.setAdapter(adapter);
    }

}

My Adapter:

public class MyCustomDrinksListAdapter extends ArrayAdapter<DrinksList> {


    private List<DrinksList> items = null;
    private ArrayList<DrinksList> arraylist;
    public MyCustomDrinksListAdapter(Context context, int layoutId, List<DrinksList> items) {
        super(context, layoutId, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View arrayView = convertView;
        if(arrayView == null){
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            arrayView = vi.inflate(R.layout.list_item, parent, false);
        }

        DrinksList currentPosition = getItem(position);
        if(currentPosition != null){
            ImageView image = (ImageView)arrayView.findViewById(R.id.product_image_coffee);
            image.setImageResource(currentPosition.getImage());

            TextView name = (TextView)arrayView.findViewById(R.id.product_name_coffee);
            name.setText(currentPosition.getName());

            TextView price = (TextView)arrayView.findViewById(R.id.product_price_coffee);
            price.setText(String.format("%.2f", currentPosition.getPrice()));
        }
        return arrayView;
    }

    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        items.clear();
        if (charText.length() == 0) {
            items.addAll(arraylist);
        } else {
            for (DrinksList wp : arraylist) {
                if (wp.getName().toLowerCase(Locale.getDefault())
                        .contains(charText)) {
                    items.add(wp);
                }
            }
        }
        notifyDataSetChanged();
    }
}

I tried following the android developers example but I could not.


Solution

  • Instead of adding filter method in Adapter class. .. add it in you fragment.. like this..

        public class DrinksFragment extends Fragment {
    
        private View rootView;
        private ArrayAdapter<DrinksList> adapter;
        private List<DrinksList> drinks;
        private ListView lv;
        ArrayList<DrinksList> mAllData=new ArrayList<DrinksList>();
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            rootView = inflater.inflate(R.layout.activity_drinks_fragment, container, false);
            populateDrinksList();
            doSearch();
            return rootView;
        }
    
        private void doSearch() {
            final EditText et = (EditText)rootView.findViewById(R.id.searchListDrinks);
            et.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) {
                    String text = et.getText().toString().toLowerCase(Locale.getDefault());
                filter(text);
                }
            });
        }
    
    
    
        private void populateDrinksList() {
            drinks = new ArrayList<DrinksList>();
            drinks.add(new DrinksList(R.drawable.coca, "Coca Cola", 2.50));
            drinks.add(new DrinksList(R.drawable.cocalight, "Coca Cola Light", 2.50));
            drinks.add(new DrinksList(R.drawable.cocazero, "Coca Cola Zero", 2.50));
            drinks.add(new DrinksList(R.drawable.orange, "Fanta Orange", 2.50));
            drinks.add(new DrinksList(R.drawable.lemon, "Fanta Lemon", 2.50));
            drinks.add(new DrinksList(R.drawable.ble, "Fanta Blue", 2.50));
            drinks.add(new DrinksList(R.drawable.sprite, "Sprite", 2.50));
            drinks.add(new DrinksList(R.drawable.soda, "Soda Water", 2.50));
            drinks.add(new DrinksList(R.drawable.tonic, "Tonic Water", 2.50));
            drinks.add(new DrinksList(R.drawable.ioli, "Sparkling Water Ioli", 2.50));
            drinks.add(new DrinksList(R.drawable.perrier, "Sparkling Water Perrier", 2.50));
            drinks.add(new DrinksList(R.drawable.nero, "Still Water", 2.00));
            drinks.add(new DrinksList(R.drawable.redbull, "Red Bull", 4.00));
            drinks.add(new DrinksList(R.drawable.zelita, "Zelita", 2.50));
    
    mAllData.addAll(drinks);
            lv = (ListView)rootView.findViewById(R.id.drinksListView);
            adapter = new MyCustomDrinksListAdapter(getActivity().getApplicationContext(),        R.layout.list_item, drinks);
            lv.setAdapter(adapter);
        }
    
    
    public void filter(String charText) {
            charText = charText.toLowerCase(Locale.getDefault());
            drinks .clear();
            if (charText.length() == 0) {
                drinks.addAll(mAllData);
            } else {
                for (DrinksList wp : mAllData) {
                    if (wp.getName().toLowerCase(Locale.getDefault())
                            .contains(charText)) {
                        drinks .add(wp);
                    }
                }
            }
            notifyDataSetChanged();
        }
    
    }
    

    and you Adapter class will remain as it is(remove filter method)..

     public class MyCustomDrinksListAdapter extends ArrayAdapter<DrinksList> {
    
    
       // private List<DrinksList> items = null; // no use
        private ArrayList<DrinksList> arraylist;
        public MyCustomDrinksListAdapter(Context context, int layoutId, List<DrinksList> items) {
            super(context, layoutId, items);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View arrayView = convertView;
            if(arrayView == null){
                LayoutInflater vi;
                vi = LayoutInflater.from(getContext());
                arrayView = vi.inflate(R.layout.list_item, parent, false);
            }
    
            DrinksList currentPosition = getItem(position);
            if(currentPosition != null){
                ImageView image = (ImageView)arrayView.findViewById(R.id.product_image_coffee);
                image.setImageResource(currentPosition.getImage());
    
                TextView name = (TextView)arrayView.findViewById(R.id.product_name_coffee);
                name.setText(currentPosition.getName());
    
                TextView price = (TextView)arrayView.findViewById(R.id.product_price_coffee);
                price.setText(String.format("%.2f", currentPosition.getPrice()));
            }
            return arrayView;
        }
    
    
    }
    

    Thank you. Hope it works...!!1