this is a picture of firebase data this is the image of log which i am getting in logcat
mDatabase=FirebaseDatabase.getInstance();
mRef=mDatabase.getReference("items").child("0").child("snippet");
mChildEventListner = new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
for (DataSnapshot Snapshot : dataSnapshot.getChildren()) {
CategoriesModelClass user = dataSnapshot.getValue(CategoriesModelClass.class);
Log.d("kkk", "" + user);
title_description.add(user);
}
categoriesRecycleView.notifyDataSetChanged();
}
This is my code from activitymain
and I don't know should I fire query for title and description or it it will fetch it from the for loop?
this is the code of my model class
public class CategoriesModelClass {
String title,description;
public CategoriesModelClass(){
}
public CategoriesModelClass(String title, String description) {
this.title = title;
this.description = description;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public void setTitle(String title) {
this.title = title;
}
public void setDescription(String description) {
this.description = description;
}
}
and i am try to set this data in my recycle view i need only title and description
To get the values of description
and title
, please use the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference snippetRef = rootRef.child("items").child("0").child("snippet");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
CategoriesModelClass user = dataSnapshot.getValue(CategoriesModelClass.class);
Log.d("kkk", "" + user.getTitle());
//Get the values out of the user object
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
}
};
snippetRef.addListenerForSingleValueEvent(valueEventListener);
See, there is no need to loop over the snippet
node, and this because we need to get the data according to type of object that is stored.
If there will be more than one items in the 0
node, then please use the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference zeroRef = rootRef.child("items").child("0");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
CategoriesModelClass user = ds.getValue(CategoriesModelClass.class);
Log.d("kkk", "" + user.getTitle());
}
//Get the values out of the user object
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
}
};
zero.addListenerForSingleValueEvent(valueEventListener);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.