0

I have values in Firebase database and I just want to have it as a int variable.

enter image description here

I want to have coinsAmount in a variable in the project.

I was looking for a lot of information but could not understand because most of the information in English and I do not fully understand the translation of Google translator please tell me how to do it or throw off where it is written please

 ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        int coinsAmount = dataSnapshot.child("coinsAmount").getValue(Long.class);
        text.setText(coinsAmount);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
   uidRef.addListenerForSingleValueEvent(valueEventListener);

int coinsAmount = dataSnapshot.child("coinsAmount").getValue(Long.class); says:

Incompatible types. Required int but 'getValue' was inferred to T: Incompatible types: Long is not convertible to int

uidRef.addListenerForSingleValueEvent(valueEventListener);

says:

Cannot resolve symbol 'addListenerForSingleValueEvent

https://i.stack.imgur.com/PpU7n.png

initializing in activity

public class MenuActivity extends BaseActivity {
public TextView text;
private static final String TAG = "MenuActivity";
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("users").child(getUid());

1 Answers1

0

To get the value of your coinsAmount property, please use the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        long coinsAmount = dataSnapshot.child("coinsAmount").getValue(Long.class);
        Log.d(TAG, String.valueOf(coinsAmount));
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

The output in your logcat will be:

0
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thank you, could you advise me where I can read about this? but not in official documentation – georgiy110802 Apr 24 '19 at 13:05
  • Why "getValue(Long.class)" not "getValue(int.class)" – georgiy110802 Apr 24 '19 at 15:01
  • There is no `int.class`. Every number in a Firebase realtime database is a long and to get it as an int, you need to cast it to an int. So does the above code work? – Alex Mamo Apr 24 '19 at 15:02
  • For the first error please see my updated answer and for the second, are you sure you have the right dependencies added in your gradle file? – Alex Mamo Apr 24 '19 at 15:22
  • in build.gradle I have 3 dependencies: implementation of 'com.google.firebase: firebase-core: 16.0.7' implementation 'com.google.firebase: firebase-auth: 16.2.0' implementation 'com.google.firebase: firebase-database: 16.0.1' – georgiy110802 Apr 24 '19 at 15:28
  • It's correct. In that case, please show me how this error `Cannot resolve symbol 'addListenerForSingleValueEvent` looks like in Android Studio, as a screenshot. – Alex Mamo Apr 24 '19 at 15:29
  • I see now, all that code should be added in the `onCreate()` method (inside the above curly brace) not outside the method. Does it work now? – Alex Mamo Apr 24 '19 at 15:52
  • So could you please throw off more information about Firebase Database because I still want to link the database to Recycler View – georgiy110802 Apr 24 '19 at 16:04
  • Specifically some sort of link to the source – georgiy110802 Apr 24 '19 at 16:04
  • Yes, sure. **[This](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is a recommended way in which you can retrieve data from a Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo Apr 24 '19 at 16:06
  • Thank you, you helped me a lot – georgiy110802 Apr 24 '19 at 16:08