mercredi 15 juin 2016

Android Custom List View with pictures and sub items search view not working

I have a searchView I want to filter the resName, resLoc, resType, but unfortunately it's not working

public class caloocan  extends AppCompatActivity {
    String FIREBASE_URL = "https://restaulist1.firebaseio.com";
    Firebase firebaseRef;
    private List<caloocanDB> Restau = new ArrayList<>();
    SearchView searchView;
    @Override
    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) {
                String[] resName = new String[(int) dataSnapshot.getChildrenCount()];
                String[] resLoc = new String[(int) dataSnapshot.getChildrenCount()];
                String[] resType = new String[(int) dataSnapshot.getChildrenCount()];
                int i = 0;
                for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                    resName[i] = dataSnapshot1.child("resname").getValue().toString();
                    resLoc[i] = dataSnapshot1.child("resloc").getValue().toString();
                    resType[i] = dataSnapshot1.child("foodtype").getValue().toString();
                    Restau.add(new caloocanDB(resName[i], resLoc[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 text) {
            Restau.clear();

            for (caloocanDB element : tempRestList) {
                if (element.getResname().toLowerCase().startsWith(text) || element.getType().toLowerCase().startsWith(text) || element.getResloc().toLowerCase().startsWith(text)){
                    Restau.add(element);
                }
            }
            super.notifyDataSetChanged();
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire