简体   繁体   中英

Firebase Push in Android doesn't work

I am having a problem using push in my Android app, the examples I found on Firebase.com seems to show how to use push to create a node, but not a key/value. I can do this in the console very easly ref.push(key); but in Android I can't find a way of doing it other than this one which doesn't work, I am not sure why, is it because of the cascading?

Firebase ref1= new Firebase(loc);
ref1.addListenerForSingleValueEvent(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot snapshot) {
  if (snapshot.child(info1).exists()){
  Firebase ref2 = new Firebase(loc2);
  Firebase newinfo = ref2.push();
  newinfo.setValue(info2, new Firebase.CompletionListener() {

    @Override
    public void onComplete(FirebaseError firebaseError, Firebase firebase) {
    if (firebaseError != null) {
    } else {
    Toast.makeText(Activity.this,"Done!" , Toast.LENGTH_SHORT).show();
    }}});}}

  @Override
  public void onCancelled(FirebaseError firebaseError) {
  }});}

My app always show "Done!" but no data is written on Firebase.

I managed to solve the issue

Firebase ref2 = new Firebase(loc2);
Firebase ref3 = ref2.push();
Map<String, Object> info = new HashMap<String, Object>();
info.put(ref3.getKey(), info2);
ref2.updateChildren(info, new Firebase.CompletionListener() {
  @Override
  public void onComplete(FirebaseError firebaseError, Firebase firebase) {
  if (firebaseError != null) {
  } else {
     Toast.makeText(Activity.this, "Done!", Toast.LENGTH_SHORT).show();
  }
}});}}

@Override
public void onCancelled(FirebaseError firebaseError) {
}});}

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