Make FirebaseRecyclerAdapter get data under conditions and not all of them

前端 未结 4 810
我在风中等你
我在风中等你 2020-12-12 05:05

I need to adjust somehow the FirebaseRecyclerAdapter in order to get some data from my database and not all of them. Is there any way to achieve that? My end goal is to show

4条回答
  •  甜味超标
    2020-12-12 05:59

    You can use a ListView instead of this firebaserecycler. Check an example:

    List mylist = new ArrayList();
    
    database.child("houses").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                 for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                     //Filter your data
                     String name = postSnapshot.child("name").getValue().toString();
                     if(name.equals("test") {
                         YourObject myObject = new YourObject(name);
                         mylist.add(myObject);
                     }
                 }
    
                 updatelist();
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError2) {
    
            }
     });
    

    Update your listview

     void updatelist() {
         ListView yourListView = (ListView) findViewById(R.id.myListView);
    
         // get data from the table by the HousesAdapter
         YourAdapter customAdapter = new YourAdapter(YourClass.this, R.layout.adapter_layout, mylist);
    
         yourListView.setAdapter(customAdapter);
     }
    

    An adapter example:

    public class YourAdapter extends ArrayAdapter {
        Context context;
    
        public YourAdapter(Context context, int resource, List items) {
            super(context, resource, items);
            this.context = context;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            View v = convertView;
    
            YourObject yourObject = getItem(position);
    
            if (v == null) {
                LayoutInflater vi;
                vi = LayoutInflater.from(getContext());
                v = vi.inflate(R.layout.adapter_layout, null);
            }
    
            TextView mytextview = (TextView) v.findViewById(R.id.mytextview);
            mytextview.setText(yourObject.getName());
    
            return v;
        }
    }
    

    YourObject class

    public class YourObject {
        private String name;
    
        public YourObject(String name) {
             this.name = name;
        }
    
        public String getName() {
            return this.name;
        }
    
     }
    

    Layout

     
     
    

提交回复
热议问题