How to add all items if it doesn't match the filter?

54 Views Asked by At

I implemented a filter class in my application to filter ListView. Now I want to add all items of expressionlist in ListView when the user type in and doesn't match the filter, instead of display ListView in blank.
How can I do this? I need some help.

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

There are 2 best solutions below

0
Linh On BEST ANSWER

After you filter by search keyword. You can check the size of result array.
If it is empty, add the data that you want to display then notifyDataSetChanged

public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        expressionlist.clear();
        if (charText.length() == 0) {
            ...
        } else {
            ...
        }

        if(expressionlist.size() == 0){
           // add all items of expressionlist
        }
        notifyDataSetChanged();
    }
}
0
Md Maidul Islam On
public void filterData(String strSearch) {

        strSearch = strSearch.toLowerCase();
        arrList.clear();

        if (strSearch.isEmpty()) {
            arrList.add()//add your main arrmainlist
        } else {

            String usernameArr[] = strSearch.split(" ");

            for (String query : usernameArr) {
                for (upto your arrlist size) {
                    like if (data.getQues().toLowerCase().contains(keyWord.toLowerCase())
                        || data.getAns().toLowerCase().contains(keyWord.toLowerCase())) {
                    if (!tempList.contains(data)) {
                        tempList.add(data);
                    }
                    if (newList.size() > 0) {
                        check your temp list already in your arraylist/hashmap. 

Then Add to main arraylist/hashmap } }

            }

        }
        notifyDataSetChanged();