Firebase DatabaseReference

时间:2017-02-28 21:04:26

标签: android database firebase firebase-realtime-database

我有2个DatabaseReference:

  • 1是“活跃”的孩子
  • 2是“完成”孩子

我想让孩子从“活跃”到“完成”,但我无法做到。有什么想法?感谢。

我有这段代码:

public class SingleGameActivity extends AppCompatActivity {

private DatabaseReference mDatabaseActivGame;
private DatabaseReference mDatabaseFinishGame;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_single_game);
    game_key = getGameKey();

    mDatabaseActivGame = FirebaseDatabase.getInstance().getReference().child("Games").child("Active").child(game_key);
    mDatabaseGame.keepSynced(true);

    mDatabaseFinishGame = FirebaseDatabase.getInstance().getReference().child("Games").child("Finish").child(game_key);
    mDatabaseGame.keepSynced(true);
...

在活跃儿童中,我有game_key及其内部多个子值。

我想将它移动完成,因为recyclerview向我展示了活跃的孩子。谢谢!

1 个答案:

答案 0 :(得分:0)

以下代码将从ClassMap

中读取值
"your-project-root/Games/Active/game_key"

然后,以下代码将在// mDatabaseActivGame is still the same like in your sample mDatabaseActivGame.addListenerForSingleValueEvent(new ValueEventListener() { ... onDataChange(DataSnapshot dataSnapshot) { // data is in dataSnapshot object // do something here } ... });

中写入数据
"your-project-root/Games/Finish/game_key"

所以结合它们我们得到这样的代码:

// mDatabaseFinishGame is still the same like in your sample
mDatabaseFinishGame.setValue(yourValueHere);

我自己没有测试过,但理论上它应该可行。希望这有帮助