Allen Francisco
Allen Francisco

Reputation: 63

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();
        }
    }
}

I added breakpoint in adapter.filter(); and this is the result when i typed something in the searchView enter image description here

I want to filter the resName, resLoc, resType but nothing filters, Looking at the picture under adapter > mObjects the 0,1,2,3,4,5 are my listview items with text. I want to Filter it

Upvotes: 0

Views: 430

Answers (3)

Shubhank
Shubhank

Reputation: 21805

You are initializing the adapter empty and therefore nothing is filtered.

Change your initialisation to be done after you get data from the Firebase

final ListView list = (ListView) findViewById(R.id.listViewRest);
    //populate restau List

//inside the dataobserver
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++;
}
MyListAdapter adapter = new MyListAdapter(Restau);
list.setAdapter(adapter);

Upvotes: 0

Rajan Kali
Rajan Kali

Reputation: 12953

By which property are you filtering,you can write a method in Adapter according to that,for example your filtering with restaurant name , then write below code

private class MyListAdapter extends ArrayAdapter<caloocanDB> {
        public static ArrayList<coloocanDB> tempRestList = new ArrayList<>();
        public static ArrayList<coloocanDB> restList = new ArrayList<>();
        public MyListAdapter(ArrayList<caloocanDB> objects){
            super(caloocan.this, R.layout.caloocan_list_view, objects);        tempRestList = new ArrayList<>();
            restList = objects;
            tempRestList.addAll(objects);
        }

        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 = restList.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) {
      restList.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))
             restList.add(element);
          }
      }else{
             restList.addAll(tempRestList);
      } 
      super.notifyDataSetChanged();
   }
}

And in your searchView Listener

   searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String text) {
            return false;
        }
        @Override
        public boolean onQueryTextChange(String text) {
            adapter.filter(text);
            return false;
        }
    });

And in your Activity

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++;
                }
        final MyListAdapter adapter = new MyListAdapter(Restau);
        ListView list = (ListView) findViewById(R.id.listViewRest);
            }
            @Override
            public void onCancelled(FirebaseError firebaseError) {
            }
        });

Upvotes: 1

himanshu1496
himanshu1496

Reputation: 1921

Hi Allen you can try something like below, and the search logic inside filter class can be modified based on your search requirements, In the below code search will consider the string as result if it starts with those character sequence.

private class MyListAdapter extends ArrayAdapter<caloocanDB> {
public static ArrayList<coloocanDB> tempRestList = new ArrayList<>();
public MyListAdapter(){
    super(caloocan.this, R.layout.caloocan_list_view, Restau);              tempRestList = 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();
}
}

Upvotes: 0

Related Questions