简体   繁体   中英

Android && Firebase realtime database data retrieving and writing

I have two things: USERS and VALUES . One user can have many values. I would like to configure my Firebase realtime database as follows:

 "Users" : {
    "-LP4S9ewWAuEOlXKEkRB" : {
     "id" : "-LP4S9ewWAuEOlXKEkRB",
     "token" : "XXXXXXXXXXXXXXXXXXXXXXXXXX"
     }
    }
 "values":{
      userID: same as "id" in the users element,
      userToken: same as "token" in the users element,
      value: 13
    }

I have succeded doing the first part (saving the users) but for the values, the values of each new user of my application overwrites the ones of the others. this is my code:

final String userToken =  sharedPreferences.getString("USER_TOKEN", "N/A");
//assign a personal id to the value
String valueId = dbRef.child("Values").push().getKey();
ValueData myValue= new ValueData(valueId, dataToSend);
dbRef.child("Values").child("UserToken").setValue(userToken);

Please help me!! Thank you in advance.

Assuming that the children in your database are named values and userToken and not Values and UserToken as i see is in your code now, to solve this, please change the following line of code:

dbRef.child("Values").child("UserToken").setValue(userToken);

to

Map<String, Object> map = new HashMap<>();
map.put("userToken", userToken);
dbRef.child("values").updateChildren(map);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM