Filtering using TextWatcher on a ListView backed by a String ArrayAdapter returns empty results

The following code returns 0 views in listview on entering any character in search EditText. Following method is from activty class

private void setupList() { final ListView lv = (ListView) findViewById(; ArrayAdapter<Info> la = new MyListAdapter(this, mInfoList); lv.setAdapter(la); lv.setTextFilterEnabled(true); EditText edit = (EditText) findViewById(; edit.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } @Override public void afterTextChanged(Editable text) { Log.d("search", ""+text); ArrayAdapter<Info> la = (ArrayAdapter<Info>) lv.getAdapter(); la.getFilter().filter(text); la.notifyDataSetChanged(); } }); }

This is my adapter class

public class MyListAdapter extends ArrayAdapter<Info> { private Bitmap mDefaultProfilePic = null; Context mContext = null; public MyListAdapter(Context ctxt, ArrayList<Info> mFriendsAccounts) { super(ctxt,, mFriendsAccounts); mContext = ctxt; mDefaultProfilePic = BitmapFactory.decodeResource(ctxt.getResources(), R.drawable.face); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { LayoutInflater inf = (LayoutInflater) mContext.getSystemService(Service.LAYOUT_INFLATER_SERVICE); convertView = inf.inflate(R.layout.layout_list_view, null); } Info usr = getItem(position); ((TextView)convertView.findViewById(; ((ImageView)convertView.findViewById(; if (mImageBitmaps.get(position) != null) { ((ImageView)convertView.findViewById(; } else { ((ImageView)convertView.findViewById(; } return convertView; } }

-------------Problems Reply------------

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.

Here it says:

The returned adapter might not be the same adapter passed to setAdapter(ListAdapter) but might be a WrapperListAdapter

Could this have anything to do with your issue?

Category:android Views:0 Time:2011-08-09

Related post

Copyright (C), All Rights Reserved.

processed in 0.116 (s). 11 q(s)