Android 搜索选项不起作用?

Android Search option is not working?

android 自定义列表视图中的搜索功能无法使用。 RestListModel 是我的模型名称。

  1. 这是我在 ManActivity 上的代码:
import android.support.v7.widget.SearchView;

    private List<RestListModel> rdataList;
    private ListView listView;
    private CustomListAdapter adapter;
    SearchView search;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quick_list);
        rdataList = new ArrayList<RestListModel>();
        listView = (ListView) findViewById(R.id.rrlist);
        adapter = new CustomListAdapter(this, rdataList);
        listView.setAdapter(adapter);
        listView.setTextFilterEnabled(true);
   }
   // on create finished here

   public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menutosearch, menu);

        final MenuItem myActionMenuItem = menu.findItem(R.id.action_search);

        search = (SearchView) myActionMenuItem.getActionView();

        //*** setOnQueryTextListener ***
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {
                // TODO Auto-generated method stub

                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // TODO Auto-generated method stub
                adapter.filter(newText);
                return false;
            }
        });
        return true;
    }
  1. 适配器CLASS:
    public class CustomListAdapter extends BaseAdapter {
        private Activity activity;
        private LayoutInflater inflater;
        private List<RestListModel> rItems=null;
        private ArrayList<RestListModel> arraylist;

        public CustomListAdapter(Activity activity, List<RestListModel> restItems) {
            this.activity = activity;
            this.rItems = restItems;
            this.arraylist = new ArrayList<RestListModel>();
            this.arraylist.addAll(rItems);
        }

        /// I think something is wrong here



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

        }

        @Override
        public Object getItem(int location) {
            return rItems.get(location);
        }

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



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

            convertView.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

                }
            });

            return convertView;
        }

        //  this is the actually filter Filter method.
        public void filter(String charText) {
            charText = charText.toLowerCase(Locale.getDefault());
            rItems.clear();
            if (charText.length() == 0) {
                rItems.addAll(arraylist);
            } else {
                for (RestListModel wp : arraylist) {
                    if (wp.getName().toLowerCase(Locale.getDefault())
                            .contains(charText)) {
                        rItems.add(wp);
                    }
                }
            }
            notifyDataSetChanged();
        }
    }

您只是在与一个包含字符串的数组进行比较。我需要添加一个包含三个不同字符串字段的模型。

    public CustomListAdapter(Activity activity, List<RestListModel> restItems) {
        this.activity = activity;
        this.rItems = restItems;
        this.arraylist = new ArrayList<RestListModel>();
        this.arraylist.addAll(rItems);
    }

像这样更改CustomListAdapter

public class CustomListAdapter extends BaseAdapter {
    ...

    public CustomListAdapter(Activity activity, List<Movie> movieItems) {
        this.activity = activity;
        this.movieItems= new ArrayList<>(movieItems);
        this.arraylist = new ArrayList<>(movieItems);
    }

    ...

    public void add(Movie movie) {
        movieItems.add(movie);
        arraylist.add(movie);
    }
}

然后像这样添加:

 public void onResponse(JSONArray response) {
    ...
       // adding movie to movies array
       adapter.add(movie);
    ...
 }