将firebase数据库中的数据检索到Recycler视图中

时间:2018-05-07 10:39:22

标签: android firebase-realtime-database

我刚刚开始使用firebase实时数据库,我一直在尝试从firebase实时数据库中检索数据到recycler view,但我遇到了困难。

RecyclerViewAdapter

    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        final String date_path = arrayList.get(position).getDate();
        final String verse_path = arrayList.get(position).getVerse();
        final String link_path = arrayList.get(position).getLink();
        holder.date.setText(date_path);
        holder.verse.setText(verse_path);
holder.setItemClickListener(new ItemClickListener() {
    @Override
    public void onItemClick(View view, int position) {
        Intent intent = new Intent(activity, AudioPlayerActivity.class);
        activity.startActivity(intent);

    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

MainActivity.java

 mDatabase = FirebaseDatabase.getInstance().getReference("Global");
        mDatabase.keepSynced(true);
        mRecyclerView = findViewById(R.id.main_recyclerview);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                ArrayList<Sermon> arrayList = (ArrayList<Sermon>) dataSnapshot.getValue();
                mainRecyclerAdapter = new MainRecyclerAdapter(arrayList, MainActivity.this);
                mRecyclerView.setAdapter(mainRecyclerAdapter);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                String error = databaseError.toString();
                Toast.makeText(MainActivity.this, "Error : " + error, Toast.LENGTH_LONG).show();
            }
        });

错误:

E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.example.bnotion.blogapp, PID: 6616
   java.lang.ClassCastException: java.util.HashMap cannot be cast to java.util.ArrayList
       at com.example.bnotion.blogapp.MainActivity$1.onDataChange(MainActivity.java:56)
       at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
       at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
       at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
       at android.os.Handler.handleCallback(Handler.java:836)
       at android.os.Handler.dispatchMessage(Handler.java:103)
       at android.os.Looper.loop(Looper.java:203)
       at android.app.ActivityThread.main(ActivityThread.java:6251)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

1 个答案:

答案 0 :(得分:0)

Firebase节点是键和值的组合。这意味着阅读节点会导致Map<String, Sermon>而不是List<Sermon>

你需要循环遍历DataSnapshot的孩子并依次从每个孩子那里得到讲道:

public void onDataChange(DataSnapshot dataSnapshot) {
    ArrayList<Sermon> sermons = new ArrayList<>();
    for (DataSnapshot sermon: dataSnapshot.getChildren()) {
        sermons.add(sermon.getValue(Sermon.class));
    }

    mainRecyclerAdapter = new MainRecyclerAdapter(arrayList, MainActivity.this);
    mRecyclerView.setAdapter(mainRecyclerAdapter);
}