坚持从firebase获取数据到我的RecyclerView

时间:2017-06-21 06:58:38

标签: android firebase firebase-realtime-database firebase-authentication

我一直在尝试使用下面显示的代码从特定的user_id获取数据,但它无法正常工作并导致我的应用崩溃。任何帮助将不胜感激。

数据格式为:

"Attendances" : {

"QUvD9mBmxzVoKfRWpaVJdyrKXRv1" : {
  "-Kn8A6CIFCWh6tfWRq9f" : {
    "Date" : "21/5/2017",
    "Time" : "8:42:32"
  },

我只需要来自用户ID随机密钥的所有数据

public class MyReportActivity extends AppCompatActivity {

FirebaseDatabase database;
DatabaseReference myRef ;
List<UserModel> list;
RecyclerView recycle;
Button view;
private FirebaseAuth auth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_report);

    view = (Button) findViewById(R.id.view);
    recycle = (RecyclerView) findViewById(R.id.recycle);
    database = FirebaseDatabase.getInstance();
    auth = FirebaseAuth.getInstance();
    FirebaseUser user = auth.getCurrentUser();
    myRef = database.getInstance().getReference().child("Attendances").child(user.getUid());

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.

            if(dataSnapshot!=null) {


                list = new ArrayList<UserModel>();
                for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {

                    UserModel value = dataSnapshot1.getValue(UserModel.class);
                    UserModel fire = new UserModel();
                    String dateatt = value.getDate();
                    String timeatt = value.getTime();
                    fire.setDate(dateatt);
                    fire.setTime(timeatt);
                    list.add(fire);

                }
            }

        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.w("Hello", "Failed to read value.", error.toException());
        }
    });

错误是:

E / AndroidRuntime:FATAL EXCEPTION:main

              Process: com.attendanceapp.obare.myattendance, PID: 27471
              com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.attendanceapp.obare.myattendance.ReportPackage.UserModel
                  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.firebase.database.DataSnapshot.getValue(Unknown Source)
                  at com.attendanceapp.obare.myattendance.ReportPackage.MyReportActivity$1.onDataChange(MyReportActivity.java:64)
                  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:815)
                  at android.os.Handler.dispatchMessage(Handler.java:104)
                  at android.os.Looper.loop(Looper.java:207)
                  at android.app.ActivityThread.main(ActivityThread.java:5728)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

1 个答案:

答案 0 :(得分:0)

要获取该数据,请使用以下代码:

ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String dateatt = ds.child("Date").getValue(String.class);
            String timeatt = ds.child("Time").getValue(String.class);
        }

    }

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

希望它有所帮助。