在此Stackoverflow answer中,表示可以在ListView
中完成过滤,而不会覆盖getFilter
的{{1}}方法,而是实现ArrayAdapter
} toString
类。
我尝试过实现它,但过滤效果不正常。虽然POJO
会进行过滤,但它不会在数组中显示正确的项目。因此,例如,如果过滤器匹配ListView
中的单个行,则array
中会显示一个项目,但显示的是错误的项目。在这种情况下,始终显示数组的第一项,而不是实际匹配输入的搜索文本的项。
以下是我ListView
的代码:
ArrayAdapter
以下是我的public class TitleListingArrayAdapter extends ArrayAdapter<Title> {
private List<Title> items;
private Context context;
public TitleListingArrayAdapter(Context context, int textViewResourceId, List<Title> items) {
super(context, textViewResourceId, items);
this.items = items;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.titlelisting_single_row, null);
}
Title item = items.get(position);
if (item!= null) {
TextView titleView = (TextView) view.findViewById(R.id.title);
if (titleView != null) {
titleView.setText(item.getName());
}
TextView yearView = (TextView) view.findViewById(R.id.year);
if (yearView != null) {
yearView.setText(String.valueOf(item.getYear())+", ");
}
TextView genreView = (TextView) view.findViewById(R.id.genre);
if (genreView != null) {
genreView.setText(item.getGenre());
}
TextView authorView = (TextView) view.findViewById(R.id.author);
if (authorView != null) {
authorView.setText(item.getAuthor());
}
RatingBar ratingView = (RatingBar) view.findViewById(R.id.rating);
if (ratingView != null) {
ratingView.setRating(item.getRating());
}
ImageView iconView = (ImageView) view.findViewById(R.id.list_image);
iconView.setImageResource(lookupResourceId(context, item.getID()));
}
return view;
}
private int lookupResourceId(Context context, String id) {
String resourceName = "thumb_"+id;
return context.getResources().getIdentifier(resourceName, "drawable", context.getPackageName());
}
}
代码的相关部分:
Activity
Title @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listing);
databaseHandler = new DatabaseHandler(this);
listView = (ListView) findViewById(R.id.list);
List<Title> titles = databaseHandler.getAllTitles();
adapter = new TitleListingArrayAdapter(this, R.id.list, titles);
listView.setAdapter(adapter);
filterText = (EditText) findViewById(R.id.filter);
filterText.addTextChangedListener(filterTextWatcher);
}
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString().toLowerCase());
}
};
类实现toString,如下所示:
POJO
有没有人知道为什么过滤不正常以及如何解决?
答案 0 :(得分:6)
以下question处理我遇到的完全相同的问题。此问题还提供了过滤正在执行的操作的示例,通过不在列表中显示正确的项目来显示项目的正确编号。
所以看来这answer是错误的,尽管被投票了六次。我通过不使用getFilter
的{{1}}方法解决了这个问题。相反,我在ArrayAdapter
实例中创建了一个新的ArrayAdapter
,如下所示:
TextWatcher
请注意,我还将private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().equals("")) {
List<Title> filteredTitles = new ArrayList<Title>();
for (int i=0; i<titles.size(); i++) {
if (titles.get(i).toString().contains(s)) {
filteredTitles.add(titles.get(i));
}
}
adapter = new TitleListingArrayAdapter(TitleListingActivity.this, R.id.list, filteredTitles);
listView.setAdapter(adapter);
}
else {
adapter = new TitleListingArrayAdapter(TitleListingActivity.this, R.id.list, titles);
listView.setAdapter(adapter);
}
}
};
声明从List<Title> titles
中移出,并使其成为onCreate
类的成员变量,以便可以在Activity
方法中访问它onTextChanged
。