Reputation: 2401
I am trying to create a real time database with firebase, and my information isn't being stored how it should be. The JSON data is formatted as follows:
How do I get rid of the multiple instances of djProfile? And what does that random string of letters and numbers mean in that hierarchical tree?
Here is my android classes that Im using to try and retrieve this information and store it in my firebase recyclerview:
DjProfile class:
public class DjProfile
{
String djName, uniqueID;
public DjProfile(){}
public DjProfile(String djName, String uniqueID)
{
this.djName = djName;
this.uniqueID = uniqueID;
}
public String getDjName() { return djName; }
public String getUniqueID() {return uniqueID; }
}
RecyclerView information:
RecyclerView recyclerView = findViewById(R.id.dj_result_recycler); recyclerView.setLayoutManager(new LinearLayoutManager(this));
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("djProfile");
FirebaseRecyclerOptions<DjProfile> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<DjProfile>()
.setQuery(query, DjProfile.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<DjProfile, ResultsViewHolder>(firebaseRecyclerOptions)
{
@NonNull
@Override
public ResultsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.searchitem_list, viewGroup, false);
return new ResultsViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull ResultsViewHolder holder, int position, @NonNull DjProfile model) {
holder.setDjProfile(model);
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
@Override
protected void onStart()
{
super.onStart();
firebaseRecyclerAdapter.startListening();
}
@Override
protected void onStop()
{
super.onStop();
if(firebaseRecyclerAdapter != null)
{
firebaseRecyclerAdapter.stopListening();
}
}
public static class ResultsViewHolder extends RecyclerView.ViewHolder
{
private TextView djNameView;
public ResultsViewHolder(View itemView)
{
super(itemView);
djNameView = itemView.findViewById(R.id.result_dj);
}
void setDjProfile(DjProfile profile)
{
String djName = profile.getDjName();
djNameView.setText(djName);
}
}
How can I retrieve just the name inside that djProfile? What am I missing?
Any feedback is greatly appreciated!
Upvotes: 0
Views: 53
Reputation: 598728
what does that random string of letters and numbers mean in that hierarchical tree?
The key starting with -Lar
is created each time you call push()
on a reference. It's Firebase's equivalent of an array index. To learn more about them, see The 2^120 Ways to Ensure Unique Identifiers and Best Practices: Arrays in Firebase.
Upvotes: 1