Query data for FirebaseRecyclerAdapter by childs childs value

时间:2019-04-08 13:21:25

标签: java android firebase-realtime-database firebaseui

I have a Firebase structure like this for 'Messages' child:

I have a Firebase structure like this for 'Messages' child

And the last child has 'latesImage' value of true:

And the last child has 'latesImage' value of true

I am trying to create a activity where all of the contacts of a user are shown inside a RecyclerView. So under 'Messages' there is a node labeled by the users id. Under that there are all of the contacts of that specific user. In every contact there are all the messages between those two users. Is it possible to create a query for the FirebaseRecyclerAdapter to retrieve the last node from every single contact. From that node it should get the message, username and the profile image.

So the layout for one contact is like this.

I have all the necessary code for the FirebaseRecyclerAdapter to work but I don't really know how to query the data I want. Any idea how I could achieve this result?

Here is my java code for the activity:

private RecyclerView contactsRV;

private DatabaseReference messagesRef;
private FirebaseAuth firebaseAuth;
private String currentUserId;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_messages);

    contactsRV = (RecyclerView)findViewById(R.id.contactsRV);
    contactsRV.setHasFixedSize(true);
    contactsRV.setLayoutManager(new LinearLayoutManager(this));

    firebaseAuth = FirebaseAuth.getInstance();
    currentUserId = firebaseAuth.getCurrentUser().getUid().toString();
    messagesRef = FirebaseDatabase.getInstance().getReference().child("Messages");

    Query searchContacts = messagesRef.child(currentUserId).orderByChild("latestMessage").equalTo(true); //<--- This is the part I'm struggling with

    FirebaseRecyclerAdapter<Contacts, FindContactsViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Contacts, FindContactsViewHolder>
            (Contacts.class, R.layout.message_contacts_layout, FindContactsViewHolder.class, searchContacts)
    {
        @Override
        protected void populateViewHolder(FindContactsViewHolder viewHolder, Contacts model, int position)
        {
            viewHolder.setReceiversProfileImage(model.getReceiversProfileImage());
            viewHolder.setLatestMessage(model.getLatestMessage());
            viewHolder.setReceiversUsername(model.getReceiversUsername());
        }
    };
    contactsRV.setAdapter(firebaseRecyclerAdapter);
}


//GO BACK TO POST FEED
public void goBack(View v)
{
    toMainActivity();
}


//SEND USER TO MAIN ACTIVITY
private void toMainActivity()
{
    Intent mainIntent = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(mainIntent);
}


public static class FindContactsViewHolder extends RecyclerView.ViewHolder
{
    View mView;

    public FindContactsViewHolder(View itemView)
    {
        super(itemView);
        this.mView = itemView;
    }

    public void setReceiversProfileImage(String receiversProfileImage)
    {
        CircleImageView contactProfPic = (CircleImageView)mView.findViewById(R.id.messageContactLayoutProfPic);
        Picasso.get().load(receiversProfileImage).into(contactProfPic);
    }

    public void setLatestMessage(String latestMessage)
    {
        TextView lastMessage = (TextView)mView.findViewById(R.id.messageContactLayoutLastMsg);
        lastMessage.setText(latestMessage);
    }

    public void setReceiversUsername(String receiversUsername)
    {
        TextView contactUsername = (TextView)mView.findViewById(R.id.messageContactLayoutUsername);
        contactUsername.setText(receiversUsername);
    }
}

And here is the 'Contacts' java class

public String receiversProfileImage, receiversUsername, latestMessage;

public Contacts(){

}

public Contacts(String receiversProfileImage, String receiversUsername, String latestMessage) {
    this.receiversProfileImage = receiversProfileImage;
    this.receiversUsername = receiversUsername;
    this.latestMessage = latestMessage;
}

public String getReceiversProfileImage() {
    return receiversProfileImage;
}

public void setReceiversProfileImage(String receiversProfileImage) {
    this.receiversProfileImage = receiversProfileImage;
}

public String getReceiversUsername() {
    return receiversUsername;
}

public void setReceiversUsername(String receiversUsername) {
    this.receiversUsername = receiversUsername;
}

public String getLatestMessage() {
    return latestMessage;
}

public void setLatestMessage(String latestMessage) {
    this.latestMessage = latestMessage;
}

0 个答案:

没有答案