在該#1 answer,則表明過濾可以在不ListView
重寫ArrayAdapter
的getFilter
方法,而是在POJO
類實施toString
來完成。過濾使用ArrayAdapter一個ListView而不重寫用getFilter方法
我試着實現它,但過濾不能正常工作。雖然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());
}
};
標題POJO
類實現的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;
}
有沒有人有任何想法爲什麼過濾不是w正確地操作以及如何修復它?
您能否提供一些您期望的內容和您實際得到的內容? – Sam 2013-02-23 16:47:08
查看你的代碼:你應該實現ViewHolders(觀看Google的[Turbo-charge Your UI](http://www.google.com/events/io/2009/sessions/TurboChargeUiAndroidFast.html)),'toLowerCase()'是[冗餘](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.2_r1/android/widget/ArrayAdapter.java#ArrayAdapter.ArrayFilter.performFiltering %28java.lang.CharSequence%29),所以你可以刪除它,我建議在處理數據庫時使用Cursors,CursorAdapters和FilterQueryProviders。它們比將所有內容轉換爲列表要快得多。 – Sam 2013-02-23 16:59:15
謝謝,山姆,獲取信息。我現在在我的'ArrayAdapter'中實現了'ViewHolders',並且正在用'CursorAdapter'替代'ArrayAdapter'。我使用了'ArrayAdapter',因爲我是'Android'開發的新手,我發現了'ListView'示例,並且使用'ArrayAdapter'來基於我的代碼。我將研究「遊標」和「遊標適配器」。謝謝。 – BruceHill 2013-02-23 21:45:41