我想从 firebase 实时数据库显示关于 recyclerview android 的数据..但我无法将动态数据加载到适配器数组中

我想在 android 的 recyclerview 中显示数据。将从 Firebase 实时数据库中检索数据,但我遇到了一个奇怪的问题。

为了从数据库中获取所需的数据,我想在两个不同的节点读取数据库两次。

我在下面附上了我的代码,以及数据结构的可视化概览。

RecyclerView 仅显示演示值,该值被硬编码到适配器数组中。它没有显示应该从 for 循环内部添加的 eda。

请注意:我的适配器和助手都是正确的。

我在下面附上了我的代码:

`

package com.example.attendx;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatactivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.Datasnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class dashboard extends AppCompatactivity {

    RecyclerView featuredrecycler;
    RecyclerView.Adapter adapter;

    ArrayList<FeaturedHelper> admin_cls = new ArrayList<FeaturedHelper>();


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

        //Hooks Recycler view
        featuredrecycler = findViewById(R.id.featured_recycler);
        DatabaseReference Root_Ref,UserProfile_Ref;
        Root_Ref = FirebaseDatabase.getInstance().getReference();
        UserProfile_Ref = Root_Ref.child("userprofile").child(uid);

        DatabaseReference User_Sir_ji_Ref;
        User_Sir_ji_Ref = UserProfile_Ref.child("Sir_ji");

        //method created for recycler view
        featuredrecycler(User_Sir_ji_Ref,Root_Ref);

    }

    private void featuredrecycler(DatabaseReference user_Sir_ji_Ref,DatabaseReference root_Ref) {

        featuredrecycler.setHasFixedSize(true);
        featuredrecycler.setLayoutManager(new LinearLayoutManager(dashboard.this,LinearLayoutManager.HORIZONTAL,false));


        //ArrayList<FeaturedHelper> admin_cls = new ArrayList<>();

        admin_cls.add(new FeaturedHelper(R.drawable.icon,"test0","test2")); // This DEMO SHOWS


        user_Sir_ji_Ref = user_Sir_ji_Ref;
        user_Sir_ji_Ref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull Datasnapshot snapshot) {

                if(snapshot.exists()){
                    DatabaseReference ClassHeader;
                    ClassHeader = root_Ref.child("classroom_header");
                    for(Datasnapshot snapshot1 : snapshot.getchildren()){

                        Log.d("snAPSHOT",snapshot1.getvalue().toString());

                        ClassHeader.child(snapshot1.getvalue().toString()).addListenerForSingleValueEvent(new ValueEventListener() {


                            @Override
                            public void onDataChange(@NonNull Datasnapshot snapshot) {
                                String Cls_name,createdBy;
                                Cls_name = snapshot.child("Classname").getvalue().toString();
                                createdBy = snapshot.child("TeacherName").getvalue().toString();

                                admin_cls.add(new FeaturedHelper(R.drawable.icon,Cls_name,createdBy)); // THIS DOEsnOT SHOWS

                                Log.d("snAPSHOT",Cls_name+"\n"+createdBy);

                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError error) {

                                Toast.makeText(getapplicationContext(),"Recycler View Error.",Toast.LENGTH_SHORT).show();

                            }
                        });

                    }
                }

            }


            @Override
            public void onCancelled(@NonNull DatabaseError error) {

                Toast.makeText(getapplicationContext(),"UNABLE_TO_READ_SIR_JI",Toast.LENGTH_SHORT).show();
            }
        });


        Log.d("snAPSHOT",admin_cls.toString()); // HERE IT SHOWS ONLY ONE ELEMENT 
        adapter = new FeaturedAdapter(admin_cls);
        featuredrecycler.setadapter(adapter);

    }
}

`

请帮我解决这个奇怪的问题。

数据库结构如下链接所示:

userprofile node has sir_ji node inside,which has class_id

class_header node has node of class_id,which contains class details

cobol_12345 回答:我想从 firebase 实时数据库显示关于 recyclerview android 的数据..但我无法将动态数据加载到适配器数组中

Firebase 以异步方式工作,您应该将代码放在方法 onDataChange 中。由于您将 for loop 与内部 Firebase 连接一起使用,也许您可​​以在检索值时为其创建延迟。

for(DataSnapshot snapshot1 : snapshot.getChildren()){
   Log.d("SNAPSHOT",snapshot1.getValue().toString());
   //Here your all codes...
}
//Here you call the this function.
//1000 = 1 second
CreateDelay(1000,() -> {
   Log.d("SNAPSHOT",admin_cls.toString());
   adapter = new FeaturedAdapter(admin_cls);
   featuredrecycler.setAdapter(adapter);
});

方法CreateDelay及其接口

public static void CreateDelay(final long delayMillis,@NonNull final DelayCallback delayCallback) {
    final Looper l = Looper.myLooper();
    if (l != null) {
        new Handler(l).postDelayed(delayCallback::onFinished,delayMillis);
    }
}

@FunctionalInterface
public interface DelayCallback {
    void onFinished();
}
,

当您更改 admin_cls 中的数据时,您需要将这一事实告诉适配器,以便它知道重新渲染视图。

每当您对 adapter.notifyDataSetChanged() 中的数据进行更改(或完成更改)时,您都可以通过调用 admin_cls 来实现。

user_Sir_ji_Ref.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        if(snapshot.exists()){
            DatabaseReference ClassHeader;
            ClassHeader = root_Ref.child("classroom_header");
            for(DataSnapshot snapshot1 : snapshot.getChildren()){
                ClassHeader.child(snapshot1.getValue().toString()).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        String Cls_name,createdBy;
                        Cls_name = snapshot.child("ClassName").getValue().toString();
                        createdBy = snapshot.child("TeacherName").getValue().toString();

                        admin_cls.add(new FeaturedHelper(R.drawable.icon,Cls_name,createdBy));

                        adapter.notifyDataSetChanged(); // ? Add this
                    }
本文链接:https://www.f2er.com/2816.html

大家都在问