在 String ArrayAdapter 支持的 ListView 上使用 TextWatcher 进行过滤会返回空结果

发布于 2024-11-28 20:26:59 字数 2170 浏览 1 评论 0原文

以下代码在搜索 EditText 中输入任何字符时在列表视图中返回 0 个视图。 以下方法来自活动类

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) {

        }

        @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();
        }
    });
}

这是我的适配器类

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;
}

}

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) {

        }

        @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;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放赐 2024-12-05 20:26:59

终于解决了问题。我必须重写 Info 对象中的 toString() 方法。就我而言,过滤基于 name 字段,因此通过 toString() 返回它。

过滤过程对适配器中的每个对象调用toString()

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.

二货你真萌 2024-12-05 20:26:59

这里说:

返回的适配器可能与传递给的适配器不同
setAdapter(ListAdapter) 但可能是一个 WrapperListAdapter

这与您的问题有什么关系吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文