2013-02-23 40 views
3

在該#1 answer,則表明過濾可以在不ListView重寫ArrayAdaptergetFilter方法,而是在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正確地操作以及如何修復它?

+0

您能否提供一些您期望的內容和您實際得到的內容? – Sam 2013-02-23 16:47:08

+0

查看你的代碼:你應該實現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

+0

謝謝,山姆,獲取信息。我現在在我的'ArrayAdapter'中實現了'ViewHolders',並且正在用'CursorAdapter'替代'ArrayAdapter'。我使用了'ArrayAdapter',因爲我是'Android'開發的新手,我發現了'ListView'示例,並且使用'ArrayAdapter'來基於我的代碼。我將研究「遊標」和「遊標適配器」。謝謝。 – BruceHill 2013-02-23 21:45:41

回答

5

以下question涉及我遇到的完全相同的問題。此問題還給出了一個過濾正在執行的示例,通過在列表中未顯示正確的項目來顯示項目的正確號碼

所以看起來這answer是錯誤的,儘管被提高了六次。我沒有充分利用ArrayAdaptergetFilter方法解決了這個問題。相反,我創造我TextWatcher實例的新ArrayAdapter,如下:

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> titlesonCreate,並使其我Activity類的成員變量,以便它是裏面的訪問onTextChanged方法filterTextWatcher

+0

R.id.list是從哪裏獲得的?我對適配器有問題:http://stackoverflow.com/questions/20524417/listview-is-blank-while-using-getfilter-function – Si8 2013-12-11 21:27:25

+0

這是它的隊友!就是這個!這就是我一直在尋找的,現在我希望能完成我的面試技術任務。非常感謝! – 2015-04-26 10:37:01

相關問題