Reputation:
I'm working on a project with android and firebase real-time database. I'm not really sure how to achieve this structure for my project.
I have a list of Users (with name, email etc). I want to add one/or multiple item(s) for a single user.
So User1 can have: Item1(color: Black, percentage: 90 etc etc) Item2(...)
I am not sure if this is the correct way to structure the data or if there is a better way.
Firebase structure
And I should be able to get all items for this user and show them in a listview.
Any help how to achieve this.
Upvotes: 0
Views: 607
Reputation: 392
I would advice you to work with RecyclerView.
The following example is used as a chat:
Firstly create your ViewHolder and Data class:
public static class FirechatMsgViewHolder extends RecyclerView.ViewHolder {
TextView userTextView;
TextView emailUserTextView;
TextView msgTextView;
CircleImageView userImageView;
public FirechatMsgViewHolder(View v) {
super(v);
userTextView = (TextView) itemView.findViewById(R.id.userTextView);
emailUserTextView = (TextView) itemView.findViewById(R.id.emailUserTextView);
msgTextView = (TextView) itemView.findViewById(R.id.msgTextView);
userImageView = (CircleImageView) itemView.findViewById(R.id.userImageView);
}
}
Data Class:
public class ChatMessage {
private String text;
private String name;
private String email;
private String photoUrl;
public ChatMessage() {
}
public ChatMessage(String name, String email, String text, String photoUrl) {
this.text = text;
this.name = name;
this.photoUrl = photoUrl;
this.email = email;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhotoUrl() {
return photoUrl;
}
public void setPhotoUrl(String photoUrl) {
this.photoUrl = photoUrl;
}
}
Then add these fields into the ChatActivity:
private DatabaseReference mSimpleFirechatDatabaseReference;
private FirebaseRecyclerAdapter<ChatMessage, FirechatMsgViewHolder>
mFirebaseAdapter;
Then init all fields in your onCreate + set adapter:
//Create the reference to get data from Firebase.
mSimpleFirechatDatabaseReference = FirebaseDatabase.getInstance().getReference();
//Fill the adapter with data + add all required listeners
mFirebaseAdapter = new FirebaseRecyclerAdapter<ChatMessage,
FirechatMsgViewHolder>(
ChatMessage.class,
R.layout.chat_message,
FirechatMsgViewHolder.class,
mSimpleFirechatDatabaseReference.child("messages")) {
@Override
protected void populateViewHolder(FirechatMsgViewHolder viewHolder, ChatMessage friendlyMessage, int position) {
mProgressBar.setVisibility(ProgressBar.INVISIBLE);
viewHolder.userTextView.setText(friendlyMessage.getName());
viewHolder.emailUserTextView.setText(friendlyMessage.getEmail());
viewHolder.msgTextView.setText(friendlyMessage.getText());
if (friendlyMessage.getPhotoUrl() == null) {
viewHolder.userImageView
.setImageDrawable(ContextCompat
.getDrawable(ChatActivity.this,
R.drawable.profile));
} else {
Glide.with(ChatActivity.this)
.load(friendlyMessage.getPhotoUrl())
.into(viewHolder.userImageView);
}
}
};
mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int chatMessageCount = mFirebaseAdapter.getItemCount();
int lastVisiblePosition =
mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
if (lastVisiblePosition == -1 ||
(positionStart >= (chatMessageCount - 1) &&
lastVisiblePosition == (positionStart - 1))) {
mMessageRecyclerView.scrollToPosition(positionStart);
}
}
});
How to send data to Firebase
// The way to send data to the database. Add any required path!
mSendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ChatMessage friendlyMessage = new
ChatMessage(mUsername,
mUseremail,
"Some text",
mPhotoUrl);
mSimpleFirechatDatabaseReference.child("messages")
.push().setValue(friendlyMessage);
}
});
Upvotes: 1
Reputation:
You should structure your database as shown in below image. With this structure, you can get all items for a particular User and easily show them in ListView.
Upvotes: 0