我的 recyclerview 不显示来自 firebase 的数据

My recyclerview doesnt show data from firebase

我正在使用 java 开发一个使用 Firebase 的应用程序。我试图显示用户之前介绍过的来自 firebase 的一些数据,但它没有显示任何内容,我也不知道为什么。我把我的代码放在下面:

我的 class userPojo:

public class userPojo {

    private String tipo;
    private String titulo;
    private String avance;
    private String plataforma; 
    private String uid;

    public userPojo() {

    }

    public String getTipo() {
    return tipo;
    }

    public void setTipo(String tipo) {
    this.tipo = tipo;
    }

     public String getUid() {
    return uid;
    }

    public String getTitulo() {

    return titulo;
    }

    public void setTitulo(String titulo) {
    this.titulo = titulo;
    }

     public String getAvance() {
    return avance;
    }

    public void setAvance (String avance) {
    this.avance = avance;
    }



    public String getPlataforma() {
    return plataforma;
    }

     
    public void setPlataforma(String plataforma) {
    this.plataforma = plataforma;
    }
}

Class 适配器:

public class Adapter extends RecyclerView.Adapter<Adapter.userpojoviewholder>  {

    List<userPojo> datos;
    public Adapter(List<userPojo> datos){
        this.datos=datos;
    }


    @NonNull
    @Override
    public userpojoviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_recycler, 
    parent,false);
    userpojoviewholder holder= new userpojoviewholder(v);
    return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull userpojoviewholder holder, int position) {
        userPojo userpojo = datos.get(position);
        holder.textViewTipo.setText(userpojo.getTipo());
        holder.textViewTitulo.setText(userpojo.getTitulo());
        holder.textViewAvance.setText(userpojo.getAvance());
        holder.textViewPlataforma.setText(userpojo.getPlataforma());
    }

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

    public static class userpojoviewholder extends RecyclerView.ViewHolder{
        TextView textViewTitulo, textViewTipo,textViewAvance, textViewPlataforma;
         public userpojoviewholder(@NonNull View itemView) {
            super(itemView);
            textViewTipo=itemView.findViewById(R.id.textviewtipo);
             textViewTitulo=itemView.findViewById(R.id.textviewtitulo);
             textViewAvance=itemView.findViewById(R.id.textviewavance);
             textViewPlataforma=itemView.findViewById(R.id.textviewplataforma);
        }
        }
        }
My class visualizar:

       public class Visualizar extends AppCompatActivity {
        List<userPojo> datos;
       Adapter adapter;
       RecyclerView rv;
       String userid;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_visualizar);
       int nightModeFlags = this.getResources().getConfiguration().uiMode & 
        Configuration.UI_MODE_NIGHT_MASK;
         switch (nightModeFlags) {
         case Configuration.UI_MODE_NIGHT_YES:
            /* si esta activo el modo oscuro lo desactiva */
            AppCompatDelegate.setDefaultNightMode(
                    AppCompatDelegate.MODE_NIGHT_NO);
            break;
    /*    case Configuration.UI_MODE_NIGHT_NO:
            /* si esta desactivado el modo oscuro lo activa */
           /* AppCompatDelegate.setDefaultNightMode(
                    AppCompatDelegate.MODE_NIGHT_YES);
            break; */
    }
        userid= FirebaseAuth.getInstance().getUid();
         rv=  findViewById(R.id.recycler);
        rv.setLayoutManager(new LinearLayoutManager(this));

    datos=new ArrayList<>();

    FirebaseDatabase database= FirebaseDatabase.getInstance("https://proyecto-daniel-sanchez-default-rtdb.europe-west1.firebasedatabase.app");

    adapter=new Adapter(datos);
    rv.setAdapter(adapter);
    database.getReference().getRoot().addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot datasnapshot) {
            datos.removeAll(datos);
            for(DataSnapshot snapshot : datasnapshot.getChildren() ){
                userPojo user= snapshot.getValue(userPojo.class);
                datos.add(user);

            }
            adapter.notifyDataSetChanged();
        }

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

        }
    });
    }
}

这是我在 Firebase 上的数据库:

LogCat 错误:

2021-11-29 17:08:51.597 18298-18298/com.example.proyectofinaldanielsanchez 
D/ViewRootImpl: support adaptive color gamut feature!
2021-11-29 17:08:51.598 18298-18298/com.example.proyectofinaldanielsanchez 
V/ViewRootImpl: The specified message queue synchronization  barrier token 
has 
not been posted or has already been removed
2021-11-29 17:08:51.603 18298-18298/com.example.proyectofinaldanielsanchez 
D/ViewRootImpl[Archivos]: windowFocusChanged hasFocus=false 
inTouchMode=true
2021-11-29 17:08:51.627 18298-18298/com.example.proyectofinaldanielsanchez 
W/Choreographer: Already have a pending vsync event.  There should only be 
one 
at a time.
2021-11-29 17:08:51.649 18298-18298/com.example.proyectofinaldanielsanchez 
D/DecorView: onWindowFocusChangedFromViewRoot hasFocus: true, 
DecorView@f1d3935[Visualizar]
2021-11-29 17:08:51.652 18298-18298/com.example.proyectofinaldanielsanchez 
D/ViewRootImpl[Visualizar]: windowFocusChanged hasFocus=true 
inTouchMode=true
2021-11-29 17:09:02.011 18298-18298/com.example.proyectofinaldanielsanchez 
D/CompatibilityChangeReporter: Compat change id reported: 150939131; UID 
10617; state: ENABLED[Visualizar]

我现在卡住了,因为我找不到错误,有人可以帮助我吗? 谢谢。

在数据库的根目录下,您有两个嵌套的动态节点级别:

  1. 首先有一个关卡,好像是一个UID。
  2. 然后还有一个关卡就是push key

由于您阅读了整个数据结构,因此您还需要在 onDataChange:

中使用两个嵌套循环
database.getReference().getRoot().addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot datasnapshot) {
        datos.removeAll(datos);
        for(DataSnapshot userSnapshot : datasnapshot.getChildren() ){ //  add another loop
            for(DataSnapshot snapshot : userSnapshot.getChildren() ){
                userPojo user= snapshot.getValue(userPojo.class);
                datos.add(user);
            }
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        throw error.toException(); //  never ignore errors
    }
});