Android搜索选项不起作用?

时间:2016-10-07 21:56:53

标签: android listview search custom-lists

自定义列表视图中的android搜索功能不起作用。 RestListModel是我的模型名称。

  1. 这是我在ManActivity上的代码:
  2. 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. ADAPTER CLASS:
    2.     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();
              }
          }
      

2 个答案:

答案 0 :(得分:0)

您只与具有String的数组进行比较。我需要添加一个带有三个不同字符串字段的模型。

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

答案 1 :(得分:0)

像这样更改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);
    ...
 }