I'm trying to get the value of my firebase tree
I got a class get_categories
public class get_categories {
private String Name;
public get_categories(){
}
public String getName(){
return Name;
}
}
Then I declared the firebase
mRef = new Firebase("https://...../Users/Categories");
And try to retrieve my data with OnChildEventListener
mRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.e("Count " ,""+dataSnapshot.getChildrenCount());
//Log.e("VALUES " ,""+dataSnapshot.child("Details").getValue());
get_categories getCatName = dataSnapshot.child("Details").getValue(get_categories.class);
Log.e("NAME " ,""+getCatName.getName());
}
#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(FirebaseError firebaseError) {
}
});
But I got an error
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Name" (class com.app.pierre.myapp.get_categories), not marked as ignorable (0 known properties: ]) at [Source: java.io.StringReader#9d0f705; line: 1, column: 10] (through reference chain: com.app.pierre.myapp.get_categories["Name"])
What am I doing wrong ?
I used this case as reference:
https://www.firebase.com/docs/android/guide/retrieving-data.html
Well first change your Model like this,
public class GetCategories {
private String Name;
public GetCategories(){
}
public GetCategories(String name){
this.Name = name;
}
public String getName(){
return Name;
}
}
and Push data using Model class Constructor..
GetCategories model = new GetCategories("yourValues");
mReference.child("Details").push().setValue(model);
and Now you can get it like this way..
mRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) {
GetCategories newPost = eventSnapshot.getValue(GetCategories.class);
Log.e("NAME " ,""+ newPost.getName());
}
}
#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(FirebaseError firebaseError) {
}
});
The name of the property on your JSON starts with an uppercase letter. But by the rules of JavaBean properties, getName() correspond to a property called name (with a lowercase n). So the two don't match up.
One way to get this working is to not use a setter, but instead use a public field:
public class get_categories {
public String Name;
}
With this you're bypassing the JavaBean logic and get a direct mapping.
Related
I am trying to get UserID from one table(homeaccount) and get the user's info from another table(userinfo) using the user id. I tried to do it by the following way. But, it is overwriting the cardviews one on top of another.
databaseReference.child("homeaccount/"+globalSharedPrefs.getUserDetail("currenthome")+"/Users")
.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String str_iteration_uid = dataSnapshot.getKey();
userQuery = databaseReference.child("userinfo").orderByChild("uid").equalTo(str_iteration_uid);
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<HomeUsers, HomeUsersViewHolder>(
HomeUsers.class,
R.layout.cardview_manage_user,
HomeUsersViewHolder.class,
userQuery
){
#Override
protected void populateViewHolder(final HomeUsersViewHolder viewHolder, final HomeUsers model, int position) {
//viewHolder.setFirstName(model.getFirstName());
//viewHolder.setProfilePicture(model.getProfilePicture());
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
#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) {
}
});
Is there anyone here could help on how to retrieve those class section inside the black box and show it in listview and if i add more class section it will add automatically in my listview. Thanks
So, if your database structure is like this..
teacher{
VRdn7....f1{
name:"name"
.....
class{
key1{
--> className:"c1" }
key2{
--> className:"c2" }
key3{
--> className:"c3" }
}
}
}
And if you want values of className then,
DatabaseReference db = FirebaseDatabase.getInstance()
.getReference("teacher").child("VRdn7......f1");
db.child("class").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String className = dataSnapshot.child("className").getValue().toString();
//This will get all values of className inside "teacher\VRdn7....f1\class\***\"
// Now create adapter for listview, and then add values in a list....
}
#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) { }
});
This is my database in Firebase Database:
How can I read the value of only a1 from my Firebase Database in my Android app and store it in a String variable ra1? Example Java code using the given database would be really helpful.
Thank You.
In your case, you just want to retrieve that String ONCE, and not listen for any changes.
So, what you do:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.addListenerForSingleValueEvent(new ValueEventListener(){
#Override
public void onDataChange(DataSnapshot dataSnapshot){
String value = dataSnapshot.child("a1").getValue(String.class); //This is a1
}
});
Initiliaze root and add childeventlistener:
DatabaseReference root = FirebaseDatabase.getInstance().getReference().child("morpion_c76af");
root.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if(dataSnapshot.getKey().equals("a1")) {
String a = (String) dataSnapshot.getValue()
}
}
#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) {
}
});
Hope this helps.
I want to be able to return random objects from the following image:
And not just the first object (-Kci1vM1dVBYBcPW7QA) which returns as is already. I have inserted the code from my app where it is currently reading from. The variables one, two and three are currently reading the first object but I want them to be able to retrieve random objects.
public void displayDeals(){
databaseReference.child("FruitDeals").child("bR4sR4flMkYFrw1SzuWA8hpOnY52").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
deals_information = dataSnapshot.getValue(Deals_Information.class);
one = deals_information.getDeal();
two = deals_information.getPrice();
three = deals_information.getAisleNum();
}
#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) {
throw databaseError.toException();
}
});
}
I am trying to get the root of the Firebase with nested children which are ordered by keys. My data structure is like this:
"-Ka3rbdLvH0I8X1Lg4of" : {
"menus" : {
"-KaD6U8_n5CPbRmAhgM2" : {
"menuItems" : {
"-KaD6UEoDvrm1W1a55AR" : {
"name" : "Annemin Kahvaltısı"
},
"-KaD6UKf0DyPja-Rni0X" : {
"name" : "Sıcak Kahvaltı",
},
"-KaD6UQyrJ1p5Ok9oya4" : {
"name" : "Anadolu Kahvaltısı",
},
"name" : "Sahanda Ürünler"
},
"-KaD6VgvHIj37Qyrirw1" : {
"menuItems" : {
"-KaD6Von-sz1xqPLeI5I" : {
"name" : "Sade Omlet",
},
"-KaD6Vvg0cOBFuABbtuU" : {
"name" : "Mantarlı Omlet",
},
"name" : "Omletler"
},
I have a restaurant -> menus -> menuItems. A restaurant can have multiple menus and a menus can have multiple menuitems. They inserted with specific Firebase keys. The code, I am getting the data as
mDatabase.child("restaurants").child(lastResIdFirebaseKey).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
currentRest = snapshot.getValue(Restaurant.class);
tv_resName.setText(currentRest.getName());
tv_resDesc.setText(currentRest.getAddress());
iv_resImage.setImageResource(R.drawable.default_restaurant);
}
#Override
public void onCancelled(DatabaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
In my java class implementations; I am mapping the child nodes to hasmap like;
public HashMap<String,Menu> menus;
The Restaurant class
public class Restaurant {
private String name;
private String description;
public HashMap<String,Menu> menus;
public Restaurant() {}
public Restaurant(String name, String descriptioArrayList<Menu> menus)
{
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public HashMap<String, Menu> getMenus() {
return menus;
}
public void setMenus(HashMap<String, Menu> menus) {
this.menus = menus;
}
}
Everything works fine, I am successfully getting the restaurant root and mapping it to class. But, the menu and menuitems order are not in the same order as they are inserted from Firebase.
I have checked the datasnapshot, they are coming from Firebase unordered. I know the orderbykey() or getChildren() gets the data with the inserted order, but I am trying to do for the order both children menus and menuitems.
To sum up, I want to get whole restaurant root, its menus and menuitems the order that I inserted them. I want to do this only by getting restaurant root. Is there any way to do that? Thank you.
EDIT: Added the Restaurant.class and JSON version of the data uploaded.
Try this:
mDatabase.child("restaurants").child("restaurant_key").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ArrayList<Menu> menuArrayList = new ArrayList<>();
for(DataSnapshot menu : dataSnapshot.child("menus").getChildren()) {
ArrayList<String> menuItemArrayList = new ArrayList<>();
for(DataSnapshot menuItem : menu.child("menuItems").getChildren()) {
menuItemArrayList.add(menuItem.child("name").getValue().toString());
}
menuArrayList.add(new Menu(menu.child("name").getValue().toString(), menuItemArrayList));
}
currentRes = new Restaurant(dataSnapshot.child("name").getValue().toString(), dataSnapshot.child("description").getValue().toString(), menuArrayList);
}
#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) { }
});
If you do not understand what I'm doing please comment and I'll edit my answer.