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
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"
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>
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.
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.