开发者

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(R.id.contactList);
    ArrayAdapter<Info> la = new MyListAdapter(this, mInfoList);
    lv.setAdapter(la);
    lv.setTextFilterEnabled(true);
    EditText edit =  (EditText) findViewById(R.id.searchbar);
    edit.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

        }

开发者_如何学JAVA        @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, R.id.name, 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(R.id.name)).setText(usr.Name);
    ((ImageView)convertView.findViewById(R.id.invite)).setTag(position);

    if (mImageBitmaps.get(position) != null) {
        ((ImageView)convertView.findViewById(R.id.profilePic)).setImageBitmap(mImageBitmaps.get(position));
    } else {
        ((ImageView)convertView.findViewById(R.id.profilePic)).setImageBitmap(mDefaultProfilePic);
    }

    return convertView;
}

}


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜