在这个 Stackoverflow答案中,表明过滤可以在 a 中完成,ListView
而无需覆盖 的getFilter
方法,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
代码的相关部分:
@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());
}
};
TitlePOJO
类实现 toString 如下:
@Override
public String toString() {
String name = this.getName() == null ? "" : this.getName().toLowerCase();
String year = this.getYear() == null ? "" : this.getYear().toString();
String genre = this.getGenre() == null ? "" : this.getGenre().toLowerCase();
return name + " " +year+ " "+ genre;
}
有谁知道为什么过滤不能正常工作以及我该如何解决?