[英]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:在数据库的根目录下,您有两个嵌套的动态节点级别:
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.