Make FirebaseRecyclerAdapter get data under conditions and not all of them

前端 未结 4 800
我在风中等你
我在风中等你 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:49

    In such situations I just make set the child Views visibility inside the ViewHolder to GONE, so they disappear if they do not match the case:

    FirebaseRecyclerAdapter<Houses, HousesViewHolder> mHousesRecyclerAdapter = new FirebaseRecyclerAdapter<Houses, HousesViewHolder>(
                    Houses.class,
                    R.layout.house_single,
                    HousesViewHolder.class,
                    mDatabaseHouses
            ) {
                @Override
                protected void populateViewHolder(HousesViewHolder viewHolder, Houses house, int position) {
                    if (Integer.parseInt(house.getPrice()) > 200) {
                        viewHolder.setHouseName(house.getHouse_name());
                        viewHolder.setCity(house.getCity());
                        viewHolder.setImage(house.getMainFoto(), getApplicationContext());
                        viewHolder.setPrice(house.getPrice());
                    } else {
                        viewHolder.childView.setVisibility(View.GONE);
                        // repeat this for each childView inside your ViewHolder
                        ...
                    }
                }
            };
    
            mRecyclerView.setAdapter(mHousesRecyclerAdapter);
    

    This will work, but make sure that your item_view.xml which contains all the views from ViewHolder has appropriate setting of height: layout_height="wrap_content"

    0 讨论(0)
  • 2020-12-12 05:59

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

    List<YourObject> mylist = new ArrayList<YourObject>();
    
    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<YourObject> {
        Context context;
    
        public YourAdapter(Context context, int resource, List<Houses> 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

     <ListView
            android:id="@+id/myListView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">
     </ListView>
    
    0 讨论(0)
  • 2020-12-12 06:05

    Yes you can achieve this by key bases,

    Logic :

    You have to create key like "from_database", When you will get data from database you can just add extra key from_database = true, on the other hand you can add from server from_database= false.

    After that you can use method getviewtype() and pass type 0 : 1 on the bases of from_database key and you can exactly what you want .

    Hope it helps you.

    0 讨论(0)
  • 2020-12-12 06:07

    User Firebase Query to limit your results

    Query query = database.child(FirebasePaths.HOUSES).orderByChild(PRICE).startAt(200);
    

    This will query your database for houses whose prices are above 200.

    0 讨论(0)
提交回复
热议问题