2017-09-12 23 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