Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Firebase For Android上存储变量的正确方法_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

在Firebase For Android上存储变量的正确方法

在Firebase For Android上存储变量的正确方法,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我成功地将firebase集成到我的android应用程序中。从那时起,我已经阅读了每一篇教程、文档、技巧、信息、代码示例,当然还有对堆栈溢出的搜索,在这里我无法实现我的目标 我的老虎机游戏中有两个变量,mTokens和mSpins。现在它们在我的mainactivity中声明,所以每次打开应用程序时它们都会重置。我能够将它们存储在firebase的用户ID下。但是,我不知道当用户使用旋转或赢得令牌时,如何增加或减少它们,而不仅仅是再次设置变量。我还需要在TextView中显示这两个值,但我尝试

我成功地将firebase集成到我的android应用程序中。从那时起,我已经阅读了每一篇教程、文档、技巧、信息、代码示例,当然还有对堆栈溢出的搜索,在这里我无法实现我的目标

我的老虎机游戏中有两个变量,mTokens和mSpins。现在它们在我的mainactivity中声明,所以每次打开应用程序时它们都会重置。我能够将它们存储在firebase的用户ID下。但是,我不知道当用户使用旋转或赢得令牌时,如何增加或减少它们,而不仅仅是再次设置变量。我还需要在TextView中显示这两个值,但我尝试了无数次代码更改,这通常会使应用程序崩溃。它要么返回null,要么不是字符串。有一次它几乎可以工作了,它在create上没有更新,但有一次我用一个按钮手动调用它

因此,组织:

如何根据使用firebase存储的游戏性(加减)操作变量

如何在整个应用程序的工具栏上将存储的值显示为实时文本视图

重申一下,是的。我已经阅读了我能找到的每一个例子,不管它是否适用。我像一个老板一样在谷歌工作,所以我不得不问这个问题。如果我打算给firebase发电子邮件,他们建议在这里询问,以便将来帮助他人

这就是我的代码目前的样子,但是我已经编辑和修改了很多次。因此,我之前已经接近一个解决方案,但不知道

我还将留下firebase url,以便查看结构

Firebase ref = new Firebase("https://luckycashslots.firebaseio.com/data/users/" + MainActivity.uID + "/");
Firebase tokRef = ref.child("tokens");

//tokRef.setValue(mAuthData.getProvider());

//Tokens token = new Tokens(100);

//ref.setValue(token);

ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        if (dataSnapshot.child("tokens").getValue() != null) {
            name = (String) dataSnapshot.child("tokens").getValue().toString();
            tokens.setText(name);
            //tokens.setText(dataSnapshot.getValue().toString());
            //  Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
            // System.out.println(dataSnapshot.getValue());
            // String woot = dataSnapshot.getValue().toString();
            // tokens.setText(woot);
        }

    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

        Toast.makeText(getApplicationContext(), "couldnt update token text", Toast.LENGTH_LONG).show();

    }
});

要更改Firebase中的现有值,请使用事务

从:


请阅读整个文档,因为它涵盖了您可能遇到的许多主题和问题。

不要描述代码,而是展示一个最小的、完整的示例来说明您的问题。大多数开发人员发现阅读代码比阅读文本更容易,如果您包含我们可以首先扫描的代码,您就更有可能得到有用的答案。请参阅添加的*代码。谢谢你的建议。
Firebase upvotesRef = new Firebase("https://docs-examples.firebaseio.com/android/saving-data/fireblog/posts/-JRHTHaIs-jNPLXOQivY/upvotes");
upvotesRef.runTransaction(new Transaction.Handler() {
    @Override
    public Transaction.Result doTransaction(MutableData currentData) {
        if(currentData.getValue() == null) {
            currentData.setValue(1);
        } else {
            currentData.setValue((Long) currentData.getValue() + 1);
        }
        return Transaction.success(currentData); //we can also abort by calling Transaction.abort()
    }
    @Override
    public void onComplete(FirebaseError firebaseError, boolean committed, DataSnapshot currentData) {
        //This method will be called once with the results of the transaction.
    }
});