Search code examples
androidlistviewbaseadapter

How to filter from list view items containing image and text in custom adapter by entering text in edittext view


My motive is to filter and show items entered by user in edit textview from the list view items containing image and textview.But I am not able to filter properly (implementation is not correct i guess + newbie to Android)

I want to filter it by textview which is attached in adapter.I am able to list items properly in list view (image + text). Only thing I want is in the edit text, if I enter any words, it should filter the list view items. I have searched for the same and tried,but not successful. For now I am referring this tutorial: http://www.androidbegin.com/tutorial/android-search-filter-listview-images-and-texts-tutorial/

Any help would be appreciated. Thanks

  1. Displaylist.java

    YouTubeAdapter you;
    ArrayList<String> VideoURL=new ArrayList<String>();
    ArrayList<String> VideoID=new ArrayList<String>();
    ArrayList<String> VideoTitle=new ArrayList<String>();
    ArrayList<String> VideoThumb=new ArrayList<String>();
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_list_view);
    lv=(ListView)findViewById(R.id.lv);
    you=new YouTubeAdapter(DisplayListView.this,VideoURL,VideoTitle);
    lv.setAdapter(you);
    et_search = (EditText) findViewById(R.id.et_search);
    
    et_search.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_search.getText().toString().toLowerCase(Locale.getDefault());
            you.filter(text);
        }
    });
    }
    
  2. Adapter.java

    ArrayList<String> mVideo=new ArrayList<String>();
    ArrayList<String> mTitle=new ArrayList<String>();
    ArrayList<String> mThumb=new ArrayList<String>();
    List<String> orig;
    // Filter Class
    public void filter(String charText) {
     charText = charText.toLowerCase(Locale.getDefault());
     orig.clear();
    Log.i(TAGYTAdap,"charText:"+charText);
    if (charText.length() == 0) {
        orig.addAll(mTitle);
    } else {
        for (String wp : mTitle) {
            Log.i(TAGYTAdap,"wp:"+wp);
           if (wp.toLowerCase(Locale.getDefault())
                    .contains(charText)) {
                orig.add(wp);
            }
         }
      }
        notifyDataSetChanged();
    }
    

Solution

  • Search View and Edittext both can be use for this filter thing but Search view is better one.!

    here you go now this will work fine.! hope so

    I have done same thing here is my code.!

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

    And called it in Main.!

     // Capture Text in EditText
            editsearch.addTextChangedListener(new TextWatcher() {
    
                @Override
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                    String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
                    adapter.filter(text);
                }
    
                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1,
                                              int arg2, int arg3) {
                    // TODO Auto-generated method stub
                }
    
                @Override
                public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
                    // TODO Auto-generated method stub
                }
            });