Skip to content Skip to sidebar Skip to footer

How To Implement Search In Customlistview Based On Class Item Of Pojo Class In Android?

I have a Custom Listview. I have to display data from the webserver. I need to implement search based on input from EditText. Each row in ListView Contains Image, Title and Message

Solution 1:

You want your adapter to implement Filterable. Then override getFilter to perform the search

@OverridepublicFiltergetFilter() {
    returnnewFilter() {
        @OverrideprotectedvoidpublishResults(CharSequence constraint, FilterResults results) {
            if (results != null && results.count >= 0) {
                setData(results.values);
            } else {
                setData(mPostingData);
            }

            notifyDataSetInvalidated();
        }

        @OverrideprotectedFilterResultsperformFiltering(CharSequence constraint) {
            FilterResults result = newFilterResults();
            if (!TextUtils.isEmpty(constraint)) {
                constraint = constraint.toString().toLowerCase();
                ArrayList<PostData> currentItems = newArrayList<PostData>();
                ArrayList<PostData> foundItems = newArrayList<PostData>();

                currentItems.addAll(mPostingData);

                for (PostDatapost: currentItems){
                    // Search for the items here. If we get a match, add to the listif (post.mType.toLowerCase().contains(constraint)) {
                        foundItems.add(m);
                    } elseif .... {
                    }
                }

                result.count = foundItems.size();
                result.values = foundItems;
            } else {
                result.count = -1;
            }

            return result;
        }
    };
}

You would then call the search from the activity with adapter.getFilter().filter("Search Query").

This will replace the list of items in your listview with the search results too.

Post a Comment for "How To Implement Search In Customlistview Based On Class Item Of Pojo Class In Android?"