krikor Herlopian krikor Herlopian - 25 days ago 5
Android Question

Firebase not working

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);
}}


Simply, when I do this first time.It works the first, It stores playlist->Krikor->playlistid->playlistId/playlistName. When I however call it again, it updates the data . I just want it to store new data playlist->Krikor->new data->new data/new data. Whats the solution?

PlayId xs = new PlayId(""+playlistId,playlistName);
xs.writeNewPlayList(xs.getId(),xs);


The second issue I have, how do I read back all stored values playlist->krikor. As I might have playlist->james.

Answer

Use this code to read values

    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) {

        }
    });