简体   繁体   English

我的 recyclerview 没有显示来自 firebase 的数据

[英]My recyclerview doesnt show data from firebase

im develping an app with java which uses Firebase.我正在使用 java 开发一个应用程序,它使用 Firebase。 Im trying to show some data from firebase that previously the user has introduced previously, but it does not show anything, and i dont know why.我试图显示用户之前介绍过的 firebase 的一些数据,但它没有显示任何内容,我不知道为什么。 I put my code below:我把我的代码放在下面:

My class userPojo:我的 class 用户Pojo:

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 Adapter: 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) {

        }
    });
    }
}

this is my database on Firebase:这是我在 Firebase 上的数据库:

火力基地

LogCat error: 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]

Im stucked right now, because i can't find the error, can someone help me?我现在卡住了,因为我找不到错误,有人可以帮助我吗? Thank You.谢谢你。

Under the root of your database you have two nested dynamic levels of nodes:在数据库的根目录下,您有两个嵌套的动态节点级别:

  1. First there's a level that seems to be a UID.首先有一个似乎是 UID 的关卡。
  2. Then there is a level that is a push key.然后有一个级别是一个按键。

Since you read this entire data structure, you also need two nested loops in your onDataChange :由于您阅读了整个数据结构,因此您的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
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 RecyclerView 中显示来自 firebase 的特定数据 - Failed to show specific data from firebase in a RecyclerView 在Android的RecyclerView中显示Firebase中的数据 - Show data from Firebase in RecyclerView on Android 无法在 RecyclerView 中显示从 Firebase 检索的数据 - Failed to show data retrieved from Firebase in RecyclerView 来自 Firebase 的数据未显示在 RecyclerView 中 - Data from Firebase did not show in RecyclerView 在 android studio 中添加 textwatcher 后,我的 firebase RecyclerView 没有显示任何内容 - My firebase RecyclerView doesnt show anything after added textwatcher in android studio 我无法在 recyclerView 上显示来自我的 firebase 实时数据库的数据 - I can't show data from my firebase real-time database on a recyclerView 为什么我的 RecyclerView 不显示任何内容? - Why my RecyclerView doesnt show anything? 应用程序不会在 recyclerview 中显示来自 firebase 的数据 - App doesn't show data from firebase in recyclerview 在 Recyclerview 中显示来自 Firebase 的数据,按用户名过滤 - 如何? - Show data from Firebase in Recyclerview, filtered on username - How to? 从 Firebase 实时数据库获取的数据未显示在我的 RecyclerView 中 - Data fetched from Firebase Realtime Database is not displayed in my RecyclerView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM