简体   繁体   中英

Filter a Recyclerview using data from another activity

In an app that I'm doing I have this Recyclerview that will let the user see all the datas in my items class. Also, the Recyclerview can be filtered accordingly depending on the types of trait the user selects in another activity called find_item.activity. So the transfer of data from find_item activity to the results activity is fine but the problem now is in the filtering the recyclerview part. For the filtering Im using this methods.

public void viewResult(String hair, String color)
{
    hair=hair.toLowerCase();
    color=size.toLowerCase();
    new_list=new ArrayList<>();

    for(finditem_getItems items:arrayList)
    {
        if(items.getHair().toLowerCase().matches(hair) && items.getColor().toLowerCase().matches(color))
        {

            if(!(items.getHair().toLowerCase().contains(hair) && !(items.getColor().toLowerCase().contains(color)))){
                results.setText("Search not found");
            }
            else
            {
                new_list.add(items);
            }
        }
        else if(items.getHair().toLowerCase().matches(hair))
        {                     
             new_list.add(items);                  
        }

    }
    arrayList.clear();
    adapter.filterSearch(new_list);
}

And this is the method I use in the adapter class to filter the arraylist

public void filterSearch(ArrayList<findpet_getItems> searchLlist){
    arrayList=new ArrayList<>();
    arrayList.clear();
    arrayList.addAll(searchLlist);
    notifyDataSetChanged();
}

In the 1st method I use the datas from the other activity as parameter and use it to filter the recyclerview but it seems somethings wrong in the implementation of my filter method.

Now the problem is if the the search items doesn't exist it doesn't trigger the error statement and if the search item does exist but the two parameters doesn't match each other it just shows all the data that matches the data of the given search selection.

Can anyone tell me if what is it im doing wrong on the filtering side.

What i get from question this must resolve it .

 public void viewResult(String hair, String color) {
    hair = hair.toLowerCase();
    color = size.toLowerCase();
    new_list = new ArrayList<>();
    if(TextUtils.isEmpty(hair)  || TextUtils.isEmpty(color)){
        // To handle the case if search filers are blank
        // show the original complete list 
        new_list.addAll(arrayList);
    }else {
        for (finditem_getItems items : arrayList) {
            if (items.getHair().toLowerCase().matches(hair) || items.getColor().toLowerCase().matches(color)) 
                new_list.add(items);
        }
    }
    arrayList.clear();
    adapter.filterSearch(new_list);
    if(new_list.size==0){
        results.setText("Search not found");
    }else{
        results.setText("");
    }
}

Change the code inside loop as per your need cause i am not aware of your searching strategy.

Update : to answer to your second question

 public void viewResult(String hair, String color) {
    hair = hair.toLowerCase();
    color = size.toLowerCase();
    new_list = new ArrayList<>();
    if(TextUtils.isEmpty(hair)  && TextUtils.isEmpty(color)){
        // To handle the case if search filers are blank
        // show the original complete list
        new_list.addAll(arrayList);
    }else {
        for (finditem_getItems items : arrayList) {
            if(!TextUtils.isEmpty(hair)  && !TextUtils.isEmpty(color)){
                if (items.getHair().toLowerCase().matches(hair) && items.getColor().toLowerCase().matches(color))
                    new_list.add(items);
            }else {
                if (items.getHair().toLowerCase().matches(hair) || items.getColor().toLowerCase().matches(color))
                    new_list.add(items);
            }
        }
    }
    arrayList.clear();
    adapter.filterSearch(new_list);
    if(new_list.size==0){
        results.setText("Search not found");
    }else{
        results.setText("");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM