Not able to get the array values from firebase database

£可爱£侵袭症+ 提交于 2020-01-05 04:57:30

问题


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: "abc@gmail.com"
         | -- 1: "xyz@gmail.com"
         | -- 2: "ghi@gmail.com"
         | -- 3: "mno@gmail.com"

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)

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/43959857/not-able-to-get-the-array-values-from-firebase-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!