2017-09-12 22 views
0

我需要幫助爲當前正在處理的筆記應用程序中的列表視圖項目實現可篩選界面。我嘗試了幾種方法,但沒有得到預期的結果。現在它只是在綁定搜索項後不做任何事情。 我已經在我的適配器類和主要活動中實現了所有必要的方法。 我真的需要一些幫助,因爲我對此很陌生。感謝您期待積極的回覆。在陣列適配器上實現可篩選

// Note.java 
    public class Note implements Serializable { 

    private long mDateTime; //creation time of the note 
    private String mTitle; //title of the note 
    private String mContent; //content of the note 

    public Note(long dateInMillis, String title, String content) { 
     mDateTime = dateInMillis; 
     mTitle = title; 
     mContent = content; 
    } 


    public void setDateTime(long dateTime) { 
     mDateTime = dateTime; 
    } 

    public void setTitle(String title) { 
     mTitle = title; 
    } 

    public void setContent(String content) { 
     mContent = content; 
    } 

    public long getDateTime() { 
     return mDateTime; 
    } 


    } 

    public String getTitle() { 
     return mTitle; 
    } 

    public String getContent() { 
     return mContent; 
    } 
} 

    // ArrayAdapter which implements Filterable 




class NoteListAdapter extends ArrayAdapter<Note> implements Filterable{ 
    List<Note> objects; 
    private List<Note> mStringFilterList; 
    Filter filter; 
    private static final int WRAP_CONTENT_LENGTH = 5; 
    public NoteListAdapter(Context context, int resource, List<Note> objects) { 
     super(context, resource, objects); 
     this.objects = objects; 
     this.mStringFilterList = objects; 

    } 

    @Override 
    public int getCount() { 
     return objects.size(); 
    } 

    @Nullable 
    @Override 
    public Note getItem(int position) { 
     return objects.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 


    @Override 
    public Filter getFilter() { 
     filter = new Filter() { 
      @Override 
      protected FilterResults performFiltering(CharSequence constraint) { 
       ArrayList<Note> tempList=new ArrayList<Note>(); 
       FilterResults results = new FilterResults(); 
       if (constraint != null && objects != null) { 
        for(Note singleNote : objects) { 
         if(singleNote.getTitle().contains(constraint)) 
          tempList.add(singleNote); 
        } 
        results.values = tempList; 
        results.count = tempList.size(); 
       } 

       return results; 
      } 


    @Override 
    protected void publishResults(CharSequence constraint, FilterResults results) { 
     objects = (ArrayList<Note>) results.values; 
     notifyDataSetChanged(); 

    } 
}; 
return filter; 
} 



    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     if(convertView == null) { 
      convertView = LayoutInflater.from(getContext()) 
        .inflate(R.layout.list_component, parent, false); 
    } 

     return convertView; 
    } 


} 

// Main Activity 



    @Override 
     public boolean onQueryTextSubmit(String query) { 
      ArrayList<Note> notes = Utilities.getAllSavedNotes(getApplicationContext()); 
      final NoteListAdapter na = new NoteListAdapter(this, R.layout.list_component, notes); 
      na.getFilter().filter(query); 
      return false; 
     } 

     @Override 
     public boolean onQueryTextChange(String newText) { 
      return false; 
     } 
    } 

回答

0

你返回super.getFilter(),而不是你的過濾器剛剛創建。而且,如果CharSequence有某些內容並且適配器包含項目,則您不會過濾Filter performFiltering()方法中的任何內容,因爲您正在將每個項目添加到結果中。

我建議這樣的事情:你正在使用一個新的適配器沒有設置

此外,在活動中,裏面的

@Override 
public boolean onQueryTextSubmit(String query) { 
    ArrayList<Note> notes = Utilities.getAllSavedNotes(getApplicationContext()); 
    final NoteListAdapter na = new NoteListAdapter(this, R.layout.list_component, notes); 
    na.getFilter().filter(query); 
    return false; 
} 

protected FilterResults performFiltering(CharSequence constraint) { 
    ArrayList<Note> tempList=new ArrayList<Note>(); 
    FilterResults results = new FilterResults(); 
    if (constraint != null && objects != null) { 
     for(Note singleNote : objects) { 
     if(singleNote.getTitle().contains(constraint) 
      tempList.add(singleNote); 
     } 
     results.values = tempList; 
     results.count = tempList.size(); 
    } 

    return results; 
} 

EDITS它到recyclerView/listView。 嘗試使用您在該活動的onCreate()內創建的適配器,或嘗試使用recyclerView/listViewsetAdapter(na)後您使用na.getFilter().filter(query);


EDITS2更改適配器:

所以,如果你想,只要用戶輸入的東西從onQueryTextSubmit()代碼移到onQueryTextChanged()要執行過濾器。

對於你問的改善(當字符串是空的重載的完整列表),你有2種方式:

第一種方式:

@Override 
public boolean onQueryTextChanged(String query) { 
    if (!query.equals("")) { 
     ArrayList<Note> notes = Utilities.getAllSavedNotes(getApplicationContext()); 
     final NoteListAdapter na = new NoteListAdapter(this, R.layout.list_component, notes); 
     na.getFilter().filter(query); 
    } else { 
     ArrayList<Note> notes = Utilities.getAllSavedNotes(getApplicationContext()); 
     final NoteListAdapter na = new NoteListAdapter(this, R.layout.list_component, notes); 
    } 
    return false; 
} 

的另一種方式可能是的performFiltering()方法內Filter接口的實現。您還可以檢查字符串是否爲空,如果是這樣,則需要返回完整列表,而不創建新的支持,只有在某些條件匹配時才添加元素。

+0

我已經實現你的解決方案你的解決方案,但獲得相同的結果。你可以請檢閱我的代碼嗎? – benboo

+0

嘗試刪除'publishResults()'中的'if'語句並始終調用'notifyDataSetChanged()' –

+0

我很感謝你的幫助,但它給出了相同的結果。 – benboo