ListView is blank while using getFilter function
I modified my code and was able to get it to work. If anyone wants to use it, be my guest :)
SetRows Java file:
public class SetRows {
int image;
String name;
String id;
public int getImage () {
return image;
}
public void setImage (int image) {
this.image = image;
}
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
public String getID () {
return id;
}
public void setID (String id) {
this.id = id;
}
public SetRows(int image, String name, String id) {
super();
this.image = image;
this.name = name;
this.id = id;
}
@Override
public String toString() {
return image + " " + name + " " + id;
}
}
SetRowsCustomAdapter Java file:
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;
public class SetRowsCustomAdapter extends ArrayAdapter<SetRows> {
Context context;
int layoutResourceId;
ArrayList<SetRows> data=new ArrayList<SetRows>(); //data = countryList
private ArrayList<SetRows> originalList;
private NameFilter filter;
public SetRowsCustomAdapter(Context context, int layoutResourceId, ArrayList<SetRows> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
//this.data.addAll(data);
this.originalList = new ArrayList<SetRows>();
this.originalList.addAll(data);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ImageHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtID = (TextView)row.findViewById(R.id.txtModDate);
row.setTag(holder);
}
else
{
holder = (ImageHolder)row.getTag();
}
SetRows myImage = data.get(position);
holder.txtTitle.setText(myImage.name);
holder.txtID.setText(myImage.id);
int outImage=myImage.image;
holder.imgIcon.setImageResource(outImage);
return row;
}
static class ImageHolder
{
ImageView imgIcon;
TextView txtTitle;
TextView txtID;
}
@Override
public Filter getFilter() {
if (filter == null){
filter = new NameFilter();
}
return filter;
}
private class NameFilter extends Filter
{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
FilterResults result = new FilterResults();
if(constraint != null && constraint.toString().length() > 0)
{
ArrayList<SetRows> filteredItems = new ArrayList<SetRows>();
for(int i = 0, l = originalList.size(); i < l; i++)
{
SetRows nameList = originalList.get(i);
if(nameList.toString().toLowerCase().contains(constraint))
filteredItems.add(nameList);
}
result.count = filteredItems.size();
result.values = filteredItems;
}
else
{
synchronized(this)
{
result.values = originalList;
result.count = originalList.size();
}
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
data = (ArrayList<SetRows>)results.values;
notifyDataSetChanged();
clear();
for(int i = 0, l = data.size(); i < l; i++)
add(data.get(i));
notifyDataSetInvalidated();
}
}
}
Everything else stayed the same :)
I hope people find it useful for their own app.
Search bar to ListView return blank values and getFilter error
Add a constructor to the State class:
public State(String name) {
this.name = name;
}
Don't use the global allModelItemsArray and filteredItemsArray, get rid of them.
Then change your performFiltering function like this:
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
FilterResults result = new FilterResults();
if(constraint != null && constraint.toString().length() > 0)
{
ArrayList<State> filteredItems = new ArrayList<State>();
for(int i = 0, l = getCount(); i < l; i++)
{
State m = getItem(i);
if(m.getName().toLowerCase().contains(constraint))
filteredItems.add(m);
}
result.count = filteredItems.size();
result.values = filteredItems;
}
else
{
ArrayList<State> allItems = new ArrayList<State>();
for(int i = 0, l = getCount(); i < l; i++)
{
State m = getItem(i);
allItems.add(m);
}
synchronized(this)
{
result.values = allItems;
result.count = allItems.size();
}
}
return result;
}
This will work much better and is proper way to fix this.
how to find out if ArrayAdapter for a ListView is empty after being filtered
Are you sure this line:
mArrayAdapter.notifyDataSetChanged();
was called on UI thread? If not, your adapter will have no idea about data changes.
UPDATE
I suggest using FilterListener, here is an example:
mArrayAdapter.getFilter().filter(searchQuery, new Filter.FilterListener() {
@Override
public void onFilterComplete(int count) {
if (count == 0){
Toast.makeText(MainActivity.this, "0 item", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, String.format("Items: %d", count), Toast.LENGTH_SHORT).show();
}
}
});
Some items not filtering from ListView using getFilter
The default filter for ArrayAdapter
only filters alphabetically so if you filter on "Alaska", it will find "Alaska - Cordova" (case-insensitive BTW), but not "Cordova, Alaska".
You will need to write your own filter, and since you can't access the backing list to ArrayAdapter
, you will need to implement your own BaseAdapter
subclass which implements Filterable
as well.
Filtering a ListView using an ArrayAdapter without overriding getFilter method
The following question deals with exactly the same issue that I encountered. This question also gives an example of what the filtering is doing, showing the correct number of items by not showing the correct items in the list.
So it seems that this answer is wrong, despite being upvoted six times. I resolved this by not making use of the getFilter
method of the ArrayAdapter
at all. Rather, I create a new ArrayAdapter
in my TextWatcher
instance, as follows:
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().equals("")) {
List<Title> filteredTitles = new ArrayList<Title>();
for (int i=0; i<titles.size(); i++) {
if (titles.get(i).toString().contains(s)) {
filteredTitles.add(titles.get(i));
}
}
adapter = new TitleListingArrayAdapter(TitleListingActivity.this, R.id.list, filteredTitles);
listView.setAdapter(adapter);
}
else {
adapter = new TitleListingArrayAdapter(TitleListingActivity.this, R.id.list, titles);
listView.setAdapter(adapter);
}
}
};
Note that I also moved the declaration List<Title> titles
out of onCreate
and made it a member variable of my Activity
class so that it is accessible inside the onTextChanged
method of filterTextWatcher
.
Filtering using TextWatcher on a ListView backed by a String ArrayAdapter returns empty results
Finally fixed the problem. I had to override the toString()
method in Info
object. In my case filtering is based on name
field so returned it through toString()
.
The filtering process calls the toString()
on each object in the adapter.
Filter listview, i can't understand filter rule
There is no need to maintain the aristas
array member, as you are already passing it to the super. In the getView
method, get the item by calling getItem(position)
.. This correction will fix the problem..
Related Topics
How to Generate Random Number in Specific Range in Android
How to Create a Bks (Bouncycastle) Format Java Keystore That Contains a Client Certificate Chain
Can't Make Jdbc Connection to MySQL (Using Java, Intellij, and Linux)
Exception in Thread "Main" Java.Lang.Noclassdeffounderror: Org/Openqa/Selenium/Webdriver
Picking a Random Element from a Set
How to Mark Jtable Cell Input as Invalid
How to Read an External Properties File in Maven
Playing Mp3 Using Java Sound API
What Is the Size of a Boolean Variable in Java
Where's My Invalid Character (Ora-00911)
How to Make a Copy of a File in Android
Error: Unable to Run Mksdcard Sdk Tool
Garbage Collector Log (Loggc) File Rotation with Logrotate Does Not Work Properly
Getting Fonts, Sizes, Bold,...Etc
What's the Syntax for Mod in Java