I'm something new on Android and I'm doing an app where a list of data taken from firebase is used, which I want to show in a recyclerView and it does not show me directly, but when debugging I look for the error and with certain steps it shows me the list but with the app encouraged, maybe the error is in the adapter or when downloading the info from the database. This is the code where you do not show me the list:
FirebaseFirestore db = FirebaseFirestore.getInstance();
private RecyclerView recyclerView;
private RecyclerViewAdapter adapter;
private RecyclerView.LayoutManager manager;
FirebaseAuth mAuth;
ArrayList<Item> listaItems = new ArrayList<>();
//Aquí declaro mi adaptador y le doy la lista
adapter = new RecyclerViewAdapter(this,getListaItems());
//Método que obtiene la lista de objetos
public ArrayList<Item> getListaItems()
{
db.collection("Historias")
.get()
//Al debuguear, se detiene en esta linea y salta hasta el return, dando como resultado un 0 cuando se recibe en el adaptador.
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Map<String, Object> historias = new HashMap<>();
historias = document.getData();
String titulo = (String)historias.get("Titulo");
String contenido = (String)historias.get("Historia");
String usuario = (String)historias.get("Usuario");
listaItems.add(
new Item(
titulo,
contenido,
usuario
)
);
}
} else {
Toast.makeText(historias.this, "No se pudo actualizar las historias porque... "+task.getException(), Toast.LENGTH_SHORT).show();
}
}
});
return listaItems;
}
Adapter class:
public class RecyclerViewAdapter extends RecyclerView.Adapter{
private Context context;
private ArrayList<Item> listaItem;
//Al recibir el Array listaItem aquí me da cero
public RecyclerViewAdapter(Context context, ArrayList<Item> listaItem) {
this.context = context;
this.listaItem = listaItem;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder( ViewGroup viewGroup, int i) {
View contentView = LayoutInflater.from(context).inflate(R.layout.layout_item_lista, null);
System.out.println("CREATE VIEW HOLDER : " + i);
return new Holder(contentView);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
Item item = listaItem.get(i);
Holder holder = (Holder) viewHolder;
holder.titulo.setText(item.getTitulo());
holder.historia.setText(item.getHistoria());
holder.usuario.setText(item.getUsuario());
}
@Override
public int getItemCount() { return listaItem.size();}
public class Holder extends RecyclerView.ViewHolder {
private TextView titulo, usuario, historia;
public Holder(View v) {
super(v);
titulo = v.findViewById(R.id.ilTitulo);
historia = v.findViewById(R.id.ilHistoria);
usuario = v.findViewById(R.id.ilUsuario);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Item item = new Item(
titulo.getText().toString(),
historia.getText().toString(),
usuario.getText().toString()
);
Toast.makeText(
context,
item.getTitulo() + "\n" +
item.getHistoria() + "\n" +
item.getUsuario(),
Toast.LENGTH_SHORT
).show();
}
});
}
}
}
I hope you can help me figure out why he does not show it to me correctly.