import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class PlayId {
private DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
public String id;
public String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public PlayId() {
}
public PlayId(String id, String name) {
this.id = id;
this.name = name;
}
public void writeNewPlayList(String userId,PlayId user) { mDatabase.child("playlist").child("krikor").child(userId).setValue(user);
}
public void read()
{
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
PlayId post = dataSnapshot.getValue(PlayId.class);
System.out.println(post.getId()+" tatatat "+post.name);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
mDatabase.addValueEventListener(postListener);
}}
简单地说,当我第一次这样做时。它首先工作,它存储播放列表 - > Krikor-> playlistid-> playlistId / playlistName。然而,当我再次调用它时,它会更新数据。我只是希望它存储新数据播放列表 - > Krikor->新数据 - >新数据/新数据。什么是解决方案?
PlayId xs = new PlayId(""+playlistId,playlistName);
xs.writeNewPlayList(xs.getId(),xs);
我有第二个问题,如何读回所有存储的值playlist-> krikor。因为我可能有播放列表 - >詹姆斯。
答案 0 :(得分:3)
使用此代码读取值
DatabaseReference databaseReference1 = firebaseDatabase.getReference().child(KEY_METAINGREDIENT)
.child(KEY_META_BURGER);
final List<Ingredient> burger = new ArrayList<>();
databaseReference1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
burger.clear();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
Ingredient ingredient = dataSnapshot1.getValue(Ingredient.class);
burger.add(ingredient);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});