krsna
krsna

Reputation: 4333

Not able to get the array values from firebase database

I'm trying to check whether the signed in user is an authorized user from the JSON array in Firebase Database. How do I get the value of email field from Firebase. ?

This is the model User class:

public class User {

    ArrayList<User> email;

    public User() {}

    public ArrayList<User> getEmail() {
        return email;
    }

    public void setEmail(ArrayList<User> email) {
        this.email = email;
    }
}

For getting the email from userslist, I used this logic.

usersListDatabaseReference.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        User userEmail = dataSnapshot.getValue(User.class);
                        ArrayList<String> userEmailFromDatabase = userEmail.getEmail();
                        Log.d(TAG, "User email from Database: "+userEmailFromDatabase);
                        Log.d(TAG, "Logged in User email: " + user.getEmail());
                    }

But when I try to get the emails from the the database I get the following exception and my app crashes.

com.google.firebase.database.DatabaseException: Can't convert object of type java.util.ArrayList to type com.apvolution.amapp.model.User

How do I get an 'emailfrom theuserslistfield ? How do I loop through the entireusersList` ?

This is the database structure in my Firebase console.

amapp-731b2addclose
   + messages
   + version
   - userslist
      - email
         |
         | -- 0: "[email protected]"
         | -- 1: "[email protected]"
         | -- 2: "[email protected]"
         | -- 3: "[email protected]"

LogCat details:

05 - 13 21: 59: 37.418 16387 - 16387 / com.apvolution.bcm E / AndroidRuntime: FATAL EXCEPTION: main
Process: com.apvolution.bcm, PID: 16387
com.google.firebase.database.DatabaseException: Can 't convert object of type java.lang.String to type com.apvolution.amapp.model.User
at com.google.android.gms.internal.zzbtg.zze(Unknown Source)
at com.google.android.gms.internal.zzbtg.zzb(Unknown Source)
at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.apvolution.bcm.MainActivity$3.onDataChange(MainActivity.java: 260)
at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
at com.google.android.gms.internal.zzbqx.zzZT(Unknown Source)
at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java: 751)
at android.os.Handler.dispatchMessage(Handler.java: 95)
at android.os.Looper.loop(Looper.java: 154)
at android.app.ActivityThread.main(ActivityThread.java: 6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 757)

Upvotes: 1

Views: 1124

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138824

The probem is that you are trying to store your data in an ArrayList in stead of a Map. Change the type in Map<String, Object> map = new HashMap<>(); and your problem will be solved.

To get you data out from the dataSnapshot object, please use this code:

DatabaseReference yourRef = FirebaseDatabase.getInstance().getReference().child("userslist").child("email");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String firstEmail = (String) dataSnapshot.child("0").getValue();
        String secondEmail = (String) dataSnapshot.child("1").getValue();
        String thirdEmail = (String) dataSnapshot.child("3").getValue();
        String fourthEmail = (String) dataSnapshot.child("4").getValue();
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
yourRef.addListenerForSingleValueEvent(eventListener);

Hope it helps.

Upvotes: 1

Mercato
Mercato

Reputation: 569

Seems DataSnapshot's getValue(GenericTypeIndicator) requires you to submit a List<YourCustomModel> rather than just YourCustomModel, see this link from Firebase docs. This means you should be doing:

GenericTypeIndicator<List<User>> t = new GenericTypeIndicator<List<User>>() {};
List<User> userEmail = dataSnapshot.getValue(t);

And then, from there, retrieve each email individually. You might want to recheck how you do your User model though, since it might not need it's own ArrayList<>

Upvotes: 0

Related Questions