I trying to filter listview with custom adapter. When I start typing on edittext the list view should be filtered. Below is the code of adapter. The custom object has overridden method toString which return the name of the facebook user. I didn’t override ArrayAdapter methods getItem,add or remove.
public class FacebookFriendsAdapter extends ArrayAdapter<FacebookUser> {
private Activity activity;
public ImageLoader imageLoader;
private HashMap<String, Integer> alphaIndexer;
private String[] sections = new String[0];
private boolean enableSections;
public FacebookFriendsAdapter(Context context, int textViewResourceId,ArrayList<FacebookUser> newItems) {
super(context, textViewResourceId, newItems);
imageLoader = new ImageLoader(context.getApplicationContext());
}
public static class ViewHolder {
public TextView text;
public ImageView image;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
FacebookUser fcbu = getItem(position);
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = vi.inflate(R.layout.facebook_friendslist_row, null);
final ViewHolder holder = new ViewHolder();
holder.text = (TextView) rowView.findViewById(R.id.facebook_rowFriendName);
holder.image = (ImageView) rowView.findViewById(R.id.facebookRowUserFoto);
rowView.setTag(holder);
} else {
rowView = convertView;
}
ViewHolder holder = (ViewHolder) rowView.getTag();
String imageurl = "http://graph.facebook.com/" + fcbu.getId()+ "/picture";
holder.text.setText(fcbu.getName());
holder.image.setTag(imageurl);
imageLoader.DisplayImage(imageurl, holder.image);
return rowView;
}
}
On the activity which hold EditText I created a TextWatcher like:
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) {
facebookAdapter.getFilter().filter(s);
}
};
Everything works fine expect that after the list is filtered the filtered items are duplicated.
A would appreciate any idea what may cause this.
Thanks
I solved the problem thanks to How to write a custom filter for ListView with ArrayAdapter.
Below is the adapted custom Adapter
I also loaded facebook friends on AsyncTask so I changed that way so adapter is populated in postExecute:
Now the issue is gone. Thanks