I have a tabbed layout with 2 fragments:
1 - Form to register
2 - Format to view the registered documents
The second tab has a ListView
data, and his code to get data is:
public class PatologiaListaFragment extends Fragment {
private static final String TAG = "FragmentLista";
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
private CollectionReference usuarios;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_patologias_lista, container, false);
usuarios = db.collection("Usuarios").document(firebaseAuth.getCurrentUser().getUid().toString()).collection("Doencas");
ArrayList<Doenca> doencaArrayList = new ArrayList<Doenca>();
usuarios.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Doenca doenca = document.toObject(Doenca.class);
doencaArrayList.add(doenca); // add data to arraylist adapter
Log.i("Lets moving", doencaArrayList.toString());
Log.d(TAG, document.getId() + " => " + document.getData());
}
ListView listView = (ListView) root.findViewById(R.id.listViewCadastro);
ArrayAdapter adapter = new DoencaAdapter(getActivity(), doencaArrayList);
listView.setAdapter(adapter); //arrayadapter set
} else {
Log.d(TAG, "Erro ao puxar documentos", task.getException());
}
}
});
return root;
}
}
I receive data to ListView
adapter from my server on Firebase, when I close and open the app, the listview refresh, but, I need that whenever you move to one side and forward(before tab), you need to update the ListView adapter.
Is possible? Or I need to migrate to RecyclerView
?