Holly Ikki
Holly Ikki

Reputation: 199

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

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();
    }
}

Upvotes: 1

Views: 52

Answers (2)

Md Maidul Islam
Md Maidul Islam

Reputation: 2304

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();

Upvotes: 0

Linh
Linh

Reputation: 60923

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();
    }
}

Upvotes: 1

Related Questions