保存和读取Integer Firestore Android [英] Save and Read Integer Firestore Android

查看:53
本文介绍了保存和读取Integer Firestore Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的Android游戏创建一个简单的Stats活动。我正在使用新的Firestore数据库。我已经能够将文档的总得分,最近得分,平均得分,游戏总数和高得分保存到我的Firestore数据库中,但是当我尝试从数据库中读取数据时,它将返回null。

I'm trying to create a simple Stats activity for my Android game. I'm using the new Firestore database. I have been able to save a document to my Firestore database with a total score, recent score, avg score, total games, and high score, but when i try to read the data back from the database it returns a null.

public void readFromDB(){
    statsDoc.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            if(documentSnapshot.exists()){
                UserData userdata = documentSnapshot.toObject(UserData.class);
            }
        }

我的Userdata类:

My Userdata class:

/* Copyright statement */

package com.squidstudios.android.balloonpopper.utils;

import com.google.firebase.database.IgnoreExtraProperties;

/**
 * UserData.java
 *
 * This class represents a single user's data including username, email, number of points earned,
 * a saved state of their progress towards earning a single point, and their password.
 */
@IgnoreExtraProperties
public class UserData {
    public String email;
    public int total_points;

public int avg_score;
public int high_score;
public int total_games;
public int prev_score;

public UserData() {
    // Default constructor required for calls to DataSnapshot.getValue(UserData.class)
}


public int getTotal_games() {
    return total_games;
}

public void setTotal_games(int total_games) {
    this.total_games = total_games;
}

public int getTotal_points() {
    return total_points;
}

public void setTotal_points(int total_points) {
    this.total_points = total_points;
}

public int getAvg_score() {
    return avg_score;
}

public void setAvg_score(int avg_score) {
    this.avg_score = avg_score;
}

public int getHigh_score() {
    return high_score;
}

public void setHigh_score(int high_score) {
    this.high_score = high_score;
}


public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public int getPrev_score() {
    return prev_score;
}

public void setPrev_score(int prev_score) {
    this.prev_score = prev_score;
}

}

在我测试时效果很好用字符串输出,但由于某些原因,整数不起作用。

It works fine when I test it out with string but for some reason integers are not working. Thanks in advance!

推荐答案

经过一些测试,我得出结论,我们将不得不将Longs转换为Integers,因为这就是我们从firestore中获取数字的结果。
这是我的数据:




确保通过单击字段末尾的编辑图标来存储数字:




使用此命令进行转换:安全转换很久才可以用Java进行诠释


我的代码如下:

After doing some tests I got to the conclussion we're gonna have to cast Longs to Integers, since that's what we get from fetching a "Number" from firestore. Here's what my data looks like:


Make sure you stored a "Number" by clicking on the "edit" icon at the end of the field:


Use this to do the casting: Safely casting long to int in Java

My code looks like this:

int money = snapshot.getLong("money").intValue();



随时要求进一步的解释!


Feel free to ask for further explanation!

这篇关于保存和读取Integer Firestore Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆