0

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);
        }
Proversion
  • 77
  • 1
  • 8

1 Answers1

0

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,

Amrish Kakadiya
  • 974
  • 1
  • 7
  • 25