I have a list of students shown on a RecyclerView and retrieving data from Firebase I want to check a checkbox if they are inside a group. It apparently works, but only in Debug mode.
Whenever I try to do a normal run it does nothing (only shows the list, but none of the checkboxes are checked).
@Override
protected void onBindViewHolder(@NonNull ListViewAdapter.ViewHolder holder, int position, @NonNull Estudiante estudiante) {
DocumentSnapshot documentSnapshot = getSnapshots().getSnapshot(holder.getAbsoluteAdapterPosition());
String id = documentSnapshot.getId();
holder.nombre.setText(estudiante.getNombre());
estudiantesFirebase = editGrupoFragment.getEstudiantesFirebase();
Log.d("estList","Estudiantes Fireb: "+ estudiantesFirebase.toString());
firestore.collection("Estudiantes")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
ArrayList<String> list = new ArrayList<>();
for (QueryDocumentSnapshot document : task.getResult()) {
list.add(document.get("nombre").toString());
}
for (int i = 0; i < estudiantesFirebase.size(); i++){
if(estudiantesFirebase.get(i).equals(holder.nombre.getText().toString())){
Log.d("estList", estudiantesFirebase.get(i) + " => " + holder.nombre.getText().toString());
//TODO: quemar algo
holder.checkBox.setChecked(true);
}
}
} else {
Log.d("estList", "Error getting documents: ", task.getException());
}
}
});
}
Apparently, the ArrayList estudiantesFirebase has no values inside. The other class I am using (to get that array) is the following:
public class EditGrupoFragment extends DialogFragment{
private FragmentEditGrupoBinding binding;
private ListviewItemBinding bindingLV;
TextView txtNombreGrupo, txtNDificultadGrupo;
Spinner spinnerDificultadEditar;
Button btnGuardarCambios;
ListViewAdapter eAdapter;
RecyclerView recyclerView;
FirebaseFirestore firestore;
SearchView searchBar;
Query query;
ArrayList<String> estudiantesFirebase = new ArrayList<>();
String NGRUPO;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments()!=null){
NGRUPO = getArguments().getString("nombre");
}
}
@SuppressLint("NotifyDataSetChanged")
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
binding = FragmentEditGrupoBinding.inflate(inflater, container, false);
View root = binding.getRoot();
firestore = FirebaseFirestore.getInstance();
searchBar = root.findViewById(R.id.searchBar);
btnGuardarCambios = root.findViewById(R.id.btnGuardarEditGrupo);
txtNombreGrupo = root.findViewById(R.id.etEditNombreGrupo);
txtNDificultadGrupo = root.findViewById(R.id.txtNDificultadGrupo);
spinnerDificultadEditar= root.findViewById(R.id.spinnerDificultadEditar);
getGrupo();
setUpRecyclerView(root);
search_view();
return root;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
btnGuardarCambios.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String nombre = txtNombreGrupo.getText().toString();
String dificultad = spinnerDificultadEditar.getSelectedItem().toString();
//String description = txtDescriptionGrupo.getText().toString();
updateGrupo(nombre, dificultad);
}
});
}
private void getGrupo(){
firestore.collection("Temas").document(NGRUPO).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
String nombre = documentSnapshot.getString("nombre");
String dificultad = documentSnapshot.getString("dificultad");
estudiantesFirebase = (ArrayList<String>) documentSnapshot.get("estudiantes");
txtNombreGrupo.setText(nombre);
spinnerDificultadEditar.setPrompt("Actual: "+dificultad);
//txtDescriptionGrupo.setText(grupo);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getContext(), "Error al obtener los datos", Toast.LENGTH_SHORT).show();
}
});
}
public ArrayList<String> getEstudiantesFirebase(){
return estudiantesFirebase;
}
private void updateGrupo(String nombre, String dificultad) {
Map<String, Object> map = new HashMap<>();
map.put("nombre", nombre);
map.put("dificultad", dificultad);
firestore.collection("Temas").document(NGRUPO).update(map).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Toast.makeText(getContext(), "Tema editado", Toast.LENGTH_SHORT).show();
getDialog().dismiss();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getContext(), "Error al editar tema", Toast.LENGTH_SHORT).show();
}
});
}
@SuppressLint("NotifyDataSetChanged")
private void setUpRecyclerView(View view) {
recyclerView = view.findViewById(R.id.searchRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
query = firestore.collection("Estudiantes");
FirestoreRecyclerOptions<Estudiante> firestoreRecyclerOptions =
new FirestoreRecyclerOptions.Builder<Estudiante>().setQuery(query, Estudiante.class).build();
eAdapter = new ListViewAdapter(firestoreRecyclerOptions, getActivity(), getActivity().getSupportFragmentManager(),this);
eAdapter.notifyDataSetChanged();
recyclerView.setAdapter(eAdapter);
}
private void search_view() {
searchBar.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
textSearch(s);
return false;
}
@Override
public boolean onQueryTextChange(String s) {
textSearch(s);
return false;
}
});
}
private void textSearch(String s) {
FirestoreRecyclerOptions<Estudiante> firestoreRecyclerOptions =
new FirestoreRecyclerOptions.Builder<Estudiante>().setQuery(query.orderBy("nombre")
.startAt(s).endAt(s+"~"), Estudiante.class).build();
eAdapter = new ListViewAdapter(firestoreRecyclerOptions, getActivity(), getActivity().getSupportFragmentManager(),this);
eAdapter.startListening();
recyclerView.setAdapter(eAdapter);
}
@Override
public void onStart() {
super.onStart();
eAdapter.startListening();
}
@Override
public void onStop() {
super.onStop();
eAdapter.stopListening();
}
@Override
public void onResume() {
super.onResume();
int width = getResources().getDisplayMetrics().widthPixels;
int height = (getResources().getDisplayMetrics().heightPixels)/2;
getDialog().getWindow().setLayout(width, height);
}
}