且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Android中将Firebase子节点从一个节点移动到另一个节点?

更新时间:2022-11-25 12:06:14

我推荐使用这个:

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

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



$ b $ p
$ b

这个来源: https://gist.github.com/katowulf/6099042 。我在我的JavaEE代码和我的android应用程序中多次使用它。



您将fromPath和toPath传递给它。这是一个拷贝,而不是一个移动,所以原来也将保持在原来的位置。如果你想删除,你可以在System.out.println(Success)之后的fromPath上设置一个值。 。


I am working on a project where user request for our valet services and on the other end valet accepts request.

I am using using Firebase as backend and on request customer uid is save on 'request' child.

When valet accepts request, customer uid should move from 'request' node to 'on progress' node.

How can i do that?

I recommend using this :

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

This come from this source : https://gist.github.com/katowulf/6099042 . I used it several times in my JavaEE code and also in my android app.

You pass your fromPath and toPath. This is a copy tought and not a move, so the original will remain at his original place too. If you would like to delete, you can do a set value on the fromPath just after the System.out.println("Success"); .