SearchView过滤器不适用于自定义阵列适配器

时间:2017-10-17 19:57:01

标签: java android

我无法弄清楚为什么我无法正确使用searchView。我制作了一个记笔记的应用程序,我只想通过用户的搜索输入来过滤笔记列表。

在我的主要课程中:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nListViewNotes = (ListView) findViewById(R.id.mainListViewNotes);

    ArrayList<Note> notes = Utilities.getAllSavedNotes(this);
    SearchView searchView = (SearchView) findViewById(R.id.searchView);
    na = new NoteAdaptor(this, R.layout.item_note, notes);
    nListViewNotes.setAdapter(na);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            na.getFilter().filter(newText);

            return false;
        }
    });

  @Override
protected void onResume() {
    super.onResume();
    nListViewNotes.setAdapter(null);

    if(notes == null || notes.size() ==0){
        Toast.makeText(this, "You have no notes saved!", Toast.LENGTH_SHORT).show();
        return;
    }else{
         na = new NoteAdaptor(this, R.layout.item_note, notes);
        nListViewNotes.setAdapter(na);

        //gets the file based what posistion the user clicked on
        nListViewNotes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String fileName = ((Note) nListViewNotes.getItemAtPosition(position)).getnDateTime()
                        + Utilities.FILE_EXTENSION;

                Intent viewNoteIntent = new Intent(getApplicationContext(), NoteActivity.class);
                viewNoteIntent.putExtra("NOTE_FILE", fileName);
                startActivity(viewNoteIntent);

            }
        });
    }
}

我的自定义适配器:

public class NoteAdaptor extends ArrayAdapter<Note> {


public NoteAdaptor(@NonNull Context context, @LayoutRes int resource, @NonNull ArrayList<Note> notes) {
    super(context, resource, notes);
}

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

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

    Note note = getItem(position);

    if (note!= null){
        TextView title =  (TextView) convertView.findViewById(R.id.list_note_title);
        TextView date =  (TextView) convertView.findViewById(R.id.list_note_date);
        TextView content =  (TextView) convertView.findViewById(R.id.list_note_content);

        title.setText(note.getnTitle());
        date.setText(note.getDateTimeAsString(getContext()));

        //in case the note is very long, we only preview a substring of that note
        if (note.getnContent().length() > 50){
            content.setText(note.getnContent().substring(0, 50));
        }else{
            content.setText(note.getnContent());
        }  
    }
    return convertView;
}
}

我的XML文件:

<android.support.v7.widget.SearchView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/searchView" />

当我运行程序时,我可以在searchView中输入字母,但它不会过滤用户创建的注释。我不确定我是否做错了什么。任何帮助,将不胜感激。

0 个答案:

没有答案