Search code examples
androidobjectsearcharraylistsearchview

How to make this Adapter searchable?


This is the Adapter for my List view.. i want to add a Filter or search feature into it. please help me. But i have seen many codes but none has helped me implement it. and i have not seen a adapter which extends ArrayAdapter which has a getFilter in it. so please help me.

public class FullNameAdapter extends ArrayAdapter<Name> implements Filterable {

    public FullNameAdapter(Context context, ArrayList<Name> words){
        super(context, 0, words);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View listItemView = convertView;
        if(listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.main_listview, parent, false);
        }
        Name currentWord = getItem(position);

        TextView mainNumber = (TextView) listItemView.findViewById(R.id.mainNumber_page);
        mainNumber.setText(currentWord.getMainNumber());

        TextView sanskritName = (TextView) listItemView.findViewById(R.id.sanskrit_name_text_view_ml);
        sanskritName.setText(currentWord.getSanskritName());

        TextView scientificName = (TextView) listItemView.findViewById(R.id.scientific_name_text_view_ml);
        scientificName.setText(currentWord.getScientificName());

        TextView familyName = (TextView) listItemView.findViewById(R.id.family_text_view_ml);
        familyName.setText(currentWord.getFamilyName());

        ShapeableImageView panel_IMG_back = (ShapeableImageView) listItemView.findViewById(R.id.mainList_imageView);
        Glide.with(getContext()).load(currentWord.getPicId()).into(panel_IMG_back);
        panel_IMG_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Dialog builder = new Dialog(getContext());
                builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
                builder.getWindow().setBackgroundDrawable(
                        new ColorDrawable(android.graphics.Color.TRANSPARENT));

                builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialogInterface) {
                        //nothing;
                    }
                });

                ZoomageView imageView = new ZoomageView(getContext());
                imageView.setImageResource(currentWord.getPicId());
                builder.addContentView(imageView, new RelativeLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT));

                builder.setCanceledOnTouchOutside(true);
                builder.show();

            }
        });return listItemView;}}

Please Help!

In MAIN ACTIVITY, the Action Bar SEARCH VIEW in the Main activity, what should do? here

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.list_menu, menu);
        MenuItem menuItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) menuItem.getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                
                return true;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

These are the Objects that i added to my array list, i want to search in some of them and if the string in the array is matching then the array list can be shown in the list. but if the Name doesnt have a matching string it should go!

private void smallaaray() {
        array = new ArrayList<Name>();
        array.add(new Name("1", "akhil","", "", R.drawable.0,
                R.string.0, "",
                "","","","","","","","","","","","","",R.drawable.0,R.drawable.0,R.drawable.0,R.drawable.0));

Please Help!


Solution

  • Found A Simple & Complex Solution.

    SearchView searchView = (SearchView) menuItem.getActionView();
        searchView.setQueryHint("Search All Data!");
        searchView.setOnQueryTextListener (new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String q) {
                return false;
            }
            @Override
            public boolean onQueryTextChange(String s) {
                if (BUYED_APP == false){
                    Toast.makeText(NonDetailDrugActivity.this, "Your are Searching on a Short list of 10 Drugs Only.\nPlease Subscribe to Gain Access to Full Data!", Toast.LENGTH_LONG).show();
                }
                filter = new ArrayList<>();
                filter.clear();
                if (s.length() == 0) {
                    filter.addAll(array);
                } else {
                    for (Name post : array) {
                        String filterPattern = s.toLowerCase().trim();
                        if (post.getSanskritName().toLowerCase().contains(filterPattern)) {
                            filter.add(post);
                        } else {
                            filter.retainAll(array);
                            if (post.getScientificName().toLowerCase().contains(filterPattern)) {
                                filter.add(post);
                            } else {
                                filter.retainAll(array);
                                if (post.getFamilyName().toLowerCase().contains(filterPattern)) {
                                    filter.add(post);
                                } else {
                                    filter.retainAll(array);
                                    if (post.getOtherName().toLowerCase().contains(filterPattern)) {
                                        filter.add(post);
                                    }else {
                                        filter.retainAll(array);
                                        if (post.getAmayikaPrayogam().toLowerCase().contains(filterPattern)) {
                                            filter.add(post);
                                        }else {
                                            filter.retainAll(array);
                                            if (post.getYogas().toLowerCase().contains(filterPattern)) {
                                                filter.add(post);
                                            }else {
                                                filter.retainAll(array);
                                                if (post.getKarma().toLowerCase().contains(filterPattern)) {
                                                    filter.add(post);
                                                } else {
                                                    filter.retainAll(array);
                                                    if (post.getMalayalamNameId().toLowerCase().contains(filterPattern)) {
                                                        filter.add(post);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                adap.clear();
                adap.addAll(filter);
                adap.notifyDataSetChanged();
                return true;
            }
        });
        searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                clearSearch();
                searchView.onActionViewCollapsed();
                return true;
            }
        });
    

    &

    private void clearSearch(){
    adap.clear();
    filter.clear();
    smallaaray();
    adap.addAll(array);
    adap.notifyDataSetChanged(); }
    

    That way! I made my Search possible.! Thank you SO much for the Help Everyone.