My firebase realtime database structure looks like this:
user id 1
user id 2
request: false
seen: true
timestamp: 1526021149790
user id 3
user id 4
request: true
seen: false
timestamp: 1526021161002
Now i want to sort the recyclerview by timestamp with only items whose request value is false.
Currently i can either sort them by timestamp or request value but cannot do both. My current code is:
...................................................................................................................................................
Query conversationQueryByTimestamp = mConvDatabase.orderByChild("timestamp");
FirebaseRecyclerOptions<Conv> options =
new FirebaseRecyclerOptions.Builder<Conv>()
.setQuery(conversationQueryByTimestamp, Conv.class)
.build();
firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Conv, FirebaseUserDataViewHolder>(options) {
@Override
public FirebaseUserDataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.messageitem_layout, parent, false);
return new FirebaseUserDataViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull final FirebaseUserDataViewHolder holder, final int position, @NonNull final Conv model) {
Log.i("firesbaseCheck", "on bind view holder");
final String list_user_id = getRef(position).getKey();
Query lastMessageQuery = mMessageDatabase.child(list_user_id).limitToLast(1);
lastMessageQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String data = dataSnapshot.child("message").getValue().toString();
String time = dataSnapshot.child("time").getValue().toString();
long lastTime = Long.parseLong(time);
holder.setMessage(data, model.isSeen());
holder.setTime(getTimeAgo(lastTime));
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mUsersDatabase.child(list_user_id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final String userName = dataSnapshot.child("name").getValue().toString();
String userImage = dataSnapshot.child("image").getValue().toString();
holder.setName(userName);
holder.setUserImage(userImage, getContext());
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent chatIntent = new Intent(getContext(), MessageSingleActivity.class);
chatIntent.putExtra("user_id", list_user_id);
chatIntent.putExtra("type", "chat");
startActivity(chatIntent);
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
rvChatList.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.notifyDataSetChanged();