在 String ArrayAdapter 支持的 ListView 上使用 TextWatcher 进行过滤会返回空结果
以下代码在搜索 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
终于解决了问题。我必须重写
Info
对象中的toString()
方法。就我而言,过滤基于name
字段,因此通过toString()
返回它。过滤过程对适配器中的每个对象调用
toString()
。Finally fixed the problem. I had to override the
toString()
method inInfo
object. In my case filtering is based onname
field so returned it throughtoString()
.The filtering process calls the
toString()
on each object in the adapter.这里说:
这与您的问题有什么关系吗?
Here it says:
Could this have anything to do with your issue?