gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:04:21 25 4
gpt4 key购买 nike

以下代码在搜索 EditText 中输入任何字符时在 ListView 中返回 0 个 View 。以下方法来自 Activity 类

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

}

最佳答案

问题终于解决了。我不得不覆盖 Info 对象中的 toString() 方法。在我的例子中,过滤基于 name 字段,因此通过 toString() 返回它。
过滤过程对适配器中的每个对象调用toString()

关于android - 在 String ArrayAdapter 支持的 ListView 上使用 TextWatcher 进行过滤返回空结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6999918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com