在SearchView上实现自动过滤器

时间:2017-09-13 05:38:47

标签: java android xml searchview

我正在处理一个笔记应用程序,其中添加的笔记显示在listView中。 我将搜索栏包含为菜单项之一,以便在用户尝试搜索时过滤单个注释。 目前,我已经为我的listView实现了可过滤功能,但它没有按预期工作。当用户开始输入字母时,它不会自动过滤。

用户必须通过单击其设备上的“输入”按钮手动执行搜索功能,即使searchView为空,listView项也不会重新出现。 当用户开始键入字母时,如何确保搜索功能自动完成,就像大多数搜索功能一样?

// menumain.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context=".MainActivity">
        <item
            android:id="@+id/search"
            android:orderInCategory="100"
            app:actionViewClass= "android.support.v7.widget.SearchView"
            app:showAsAction="ifRoom"
            android:icon="@drawable/ic_search"
            android:title="@string/search" />



    </menu>

        //Main Activity
           @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String query) {
        na.getFilter().filter(query);
        if (!query.equals("")) {
            na.getFilter().filter(query);
            nb.getFilter().filter(query);
        } else {
          mListNotes.setAdapter(na);
            mGridNotes.setAdapter(nb);
        }
        return false;
    }
}
// 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;
    }
}

    // Note List Adapter

    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<>();
                    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;
        }

            }

            return convertView;
        }


    }

1 个答案:

答案 0 :(得分:0)

您还需要在onQueryTextChange方法中添加过滤代码。每次从键盘触发击键时都会调用此方法。

在您的主要活动中这样做:

//Main Activity 
@Override 
public boolean onQueryTextSubmit(String query) { 
    na.getFilter().filter(query); 
    nb.getFilter().filter(query); 
    return true; 
} 

@Override 
public boolean onQueryTextChange(String newText) {
    na.getFilter().filter(newText); 
    nb.getFilter().filter(newText); 
    return true; 
}