简体   繁体   中英

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? 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.

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. So I suggest you to put

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

inside onDataChange method like this

 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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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