使用SearchView自定义列表视图

时间:2016-06-14 15:46:28

标签: android listview arraylist searchview

我有自定义listView ArrayAdapterSearchView,但遗憾的是我无法使用listView过滤我的searchView

caloocan.java



protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.caloocan);
        Firebase.setAndroidContext(this);
        firebaseRef = new Firebase(FIREBASE_URL);
        searchView = (SearchView) findViewById(R.id.searchView);
        // populateListView();
        // populateRestauList();

        final MyListAdapter adapter = new MyListAdapter();
        ListView list = (ListView) findViewById(R.id.listViewRest);
        //populate restau List
        firebaseRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
               //DELETED CODE IS TOO LONG
               Loc[i], R.drawable.six, resType[i]));
                    i++;
                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
            }
        });
        list.setAdapter(adapter);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String text) {
                return false;
            }
            @Override
            public boolean onQueryTextChange(String text) {
                adapter.getFilter().filter(text);
                return false;
            }
        });

    }

    public class MyListAdapter extends ArrayAdapter<caloocanDB> {
        public ArrayList<caloocanDB> tempRestList = new ArrayList<>();

        public MyListAdapter() {
            super(caloocan.this, R.layout.caloocan_list_view, Restau);
            tempRestList = new ArrayList<caloocanDB>(Restau);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View caloocanView = convertView;
            if (caloocanView == null)
                caloocanView = getLayoutInflater().inflate(R.layout.caloocan_list_view, parent, false);

            caloocanDB restaurant = Restau.get(position);
            //FILL VIEW
            ImageView imageView = (ImageView) caloocanView.findViewById(R.id.imageView);
            imageView.setImageResource(restaurant.getIconID());
            // RESTAU NAME
            TextView restauName = (TextView) caloocanView.findViewById(R.id.resnameTxt);
            restauName.setText(restaurant.getResname());
            //RESTAU LOCA
            TextView location = (TextView) caloocanView.findViewById(R.id.reslocTxt);
            location.setText(restaurant.getResloc());
            //FOOD TYPE
            TextView restype = (TextView) caloocanView.findViewById(R.id.restypeTxt);
            restype.setText(restaurant.getType());
            return caloocanView;
        }

        public void filter(String filter) {
            Restau.clear();
            if(filter != null && filter.trim().length() > 0){
                for (caloocanDB element : tempRestList){
                    if (element.getResname().contains(filter) || element.getType().contains(filter) || element.getResloc().contains(filter))
                        Restau.add(element);
                }
            }else{
                Restau.addAll(tempRestList);
            }
            super.notifyDataSetChanged();
        }
    }
}
&#13;
&#13;
&#13;

0 个答案:

没有答案