2016-11-07 3 views
2

私は、私たちのバレットサービスのユーザー要求があり、反対側のバレットがリクエストを受け入れるプロジェクトに取り組んでいます。Androidのノード間でFirebaseの子を移動するにはどうすればいいですか?

私はFirebaseをバックエンドとして使用しています。要求に応じて、お客様のuidは「要求」の子に保存されます。

valetがリクエストを受け入れると、顧客のuidは 'request'ノードから 'on progress'ノードに移動する必要があります。

どうすればいいですか?

答えて

3

私は、この使用をお勧めします:https://gist.github.com/katowulf/6099042:これは、このソースから来

public void moveFirebaseRecord(Firebase fromPath, final Firebase toPath) 
{ 
    fromPath.addListenerForSingleValueEvent(new ValueEventListener() 
    { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) 
     { 
      toPath.setValue(dataSnapshot.getValue(), new Firebase.CompletionListener() 
      { 
       @Override 
       public void onComplete(FirebaseError firebaseError, Firebase firebase) 
       { 
        if (firebaseError != null) 
        { 
         System.out.println("Copy failed"); 
        } 
        else 
        { 
         System.out.println("Success"); 
        } 
       } 
      }); 
     } 

     @Override 
     public void onCancelled(FirebaseError firebaseError) 
     { 
      System.out.println("Copy failed"); 
     } 
    }); 
} 

を。私はJavaEEのコードでも、私のアンドロイドアプリでも何度も使っていました。

fromPathとtoPathを渡します。これはコピーであり、移動ではないので、オリジナルは元の場所に残っています。削除したい場合は、System.out.println( "Success")の直後にfromPathを設定してください。 。コンパイルfirebase-データベースのよう

+0

正常に動作しますが、firebase URLをパス(ref)として使用し、すべての子を移動します。私は1人の子供だけを動かしたいと思っています。 1人の子供を移動する方法。 TIA –

+0

あなたの子供のIDをお持ちの場合は、あるIDから別のIDに直接移動することができます。 –

+0

希望するもの:myfirebase.firebase.com/initialPlace/IDからmyfirebase.firebase.com/finalPlace/ID。任意の文字列を変数としてfirebaseコンストラクタに渡すことができます。 –

3

:11.0.1、これは新しいクラスの参照と同じ機能です(https://firebase.google.com/support/guides/firebase-android 2017年7月5日)

private void moveGameRoom(final DatabaseReference fromPath, final DatabaseReference toPath) { 
     fromPath.addListenerForSingleValueEvent(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       toPath.setValue(dataSnapshot.getValue(), new DatabaseReference.CompletionListener() { 
        @Override 
        public void onComplete(DatabaseError firebaseError, DatabaseReference firebase) { 
         if (firebaseError != null) { 
          System.out.println("Copy failed"); 
         } else { 
          System.out.println("Success"); 

         } 
        } 
       }); 

      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 
    } 
0

あなたもオリジナルを消去移動を実行したい場合

// In this piece of code, "fromPath" and "toPath" parameters act like directories. 
private void removeFromFirebase(final DatabaseReference fromPath, final DatabaseReference toPath, final String key) { 
    fromPath.child(key).addListenerForSingleValueEvent(new ValueEventListener() { 
     // Now "DataSnapshot" holds the key and the value at the "fromPath". 
     // Let's move it to the "toPath". This operation duplicates the 
     // key/value pair at the "fromPath" to the "toPath". 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      toPath.child(dataSnapshot.getKey()) 
        .setValue(dataSnapshot.getValue(), new DatabaseReference.CompletionListener() { 
         @Override 
         public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) { 
          if (databaseError == null) { 
           Log.i(TAG, "onComplete: success"); 
           // In order to complete the move, we are going to erase 
           // the original copy by assigning null as its value. 
           fromPath.child(key).setValue(null); 
          } 
          else { 
           Log.e(TAG, "onComplete: failure:" + databaseError.getMessage() + ": " 
             + databaseError.getDetails()); 
          } 
         } 
        }); 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 
      Log.e(TAG, "onCancelled: " + databaseError.getMessage() + ": " 
        + databaseError.getDetails()); 
     } 
    }); 
} 
関連する問題