【问题标题】:Firestore - access a number value in a documentFirestore - 访问文档中的数字值
【发布时间】:2018-07-02 19:18:30
【问题描述】:

因此,我在 Firestore 数据库中的一个文档中有一个简单的数值,我正在尝试获取该数值。到目前为止,我只为字符串对象和自定义对象做过:

String wkCategory = task.getResult().getString("setting");

所以我认为数字应该是一样的。我知道它只能获得 Long 或 Doubles,所以我尝试了这个:

int time = task.getResult().getLong("duration").intValue();

还有这个:

Number time = task.getResult().getLong("duration").intValue();

然后我将包中的值传递给下一个片段,它在 TextView 中设置。

private int time;
Bundle bundle =getArguments();
if(null!=bundle) {
   time=bundle.getInt("Time");
}
TextView time = (TextView) view.findViewById(R.id.time);
time.setText(time.toString());

但是,这就是我的 TextView 中显示的内容。正如我所说,它可以正常使用字符串。虽然无法弄清楚,因为我什至使用了

.toString OR +""

Screenshot of the TextView

【问题讨论】:

    标签: android database google-cloud-firestore


    【解决方案1】:

    您对变量名有疑问。

    确保为每个变量提供不同的名称以便理解。

    private int time;
    Bundle bundle =getArguments();
    if(null!=bundle) {
       time=bundle.getInt("Time");
    }
    TextView timeView = (TextView) view.findViewById(R.id.time);
    timeView.setText(time + "");
    

    【讨论】:

    • 很高兴它成功了。请查看名为“盒装和原始类型”和“隐式和显式转换或强制转换”的主题,以确定在哪里使用 toString() 或不使用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 2023-01-01
    • 2018-03-28
    • 1970-01-01
    相关资源
    最近更新 更多