简体   繁体   English

在textView中设置整数值显示为null,如何在android中解决此问题?

[英]Setting an integer value in textView shows null, How to solve this in android?

I fetched a value from database and stored in a variable. 我从数据库中获取了一个值,并将其存储在变量中。 I exactly got the expected value. 我完全得到了期望值。 But when I try to set those value into a textview it shows null or 0. Why this happening? 但是,当我尝试将这些值设置为textview时,它显示为null或0。为什么会这样? How to solve this? 如何解决呢?

I used, 我用了,

 claimedNos = dataSnapshot.child("Claimed").getValue(int.class);

this code for getting data from database. 此代码用于从数据库获取数据。

 valueClaimed.setText(String.valueOf(claimedNos));

this for setting value to textview. 这用于将值设置为textview。

code is here, 代码在这里,

public int claimedNos;

      uDatabase.child(userId).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

           claimedNos = dataSnapshot.child("Claimed").getValue(Integer.class);
            Log.d("TAG","claimed nos in account  :"+claimedNos);
        }

As your code posted and comments makes me assuming that you are not setting text into TextView inside listener. 正如您发布的代码和注释一样,我假设您没有在侦听器中将文本设置为TextView。 So I suggest you to put 所以我建议你放

valueClaimed.setText(String.valueOf(claimedNos));

inside onDataChange method like this 在像这样的onDataChange方法中

 public int claimedNos=0;

    uDatabase.child(userId).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

           claimedNos = dataSnapshot.child("Claimed").getValue(Integer.class);
            Log.d("TAG","claimed nos in account  :"+claimedNos);
           valueClaimed.setText(String.valueOf(claimedNos));
        }

Your code is setting value before listner listen dataChange event and it was making null and zero so now above code will set claimedNos after receiving it from firebase, 您的代码是在列表监听器侦听dataChange事件之前设置值的,它的值为null和零,所以现在上面的代码将从firebase接收到之后设置了ClaimNos

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM