im develping an app with java which uses 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. I put my code below:
My 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 Adapter:
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:
LogCat error:
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.
CodePudding user response:
Under the root of your database you have two nested dynamic levels of nodes:
- First there's a level that seems to be a UID.
- 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
:
database.getReference().getRoot().addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot datasnapshot) {
datos.removeAll(datos);
for(DataSnapshot userSnapshot : datasnapshot.getChildren() ){ //