0

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?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
luke cross
  • 321
  • 1
  • 2
  • 19

1 Answers1

1

I need that whenever you move to one side and forward(before tab), you need to update the ListView adapter.

As I understand you need to get data in real-time. In this case, you should use a real-time listener. According to official documentation regarding listen to multiple documents in a collection:

As with documents, you can use onSnapshot() instead of get() to listen to the results of a query. This creates a query snapshot.

So the key for adding the real-time feature is the use of Query's addSnapshotListener(EventListener listener) method:

Starts listening to this query.

Regarding the use of a RecyclerView, it's up to you decide which is better for you. It's true that a RecyclerView offers more features but you can check these answers out:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • ```ListView listView = (ListView) root.findViewById(R.id.listViewCadastro); ArrayAdapter adapter = new DoencaAdapter(getActivity(), doencaArrayList); listView.setAdapter(adapter); //arrayadapter set``` This methods must be change too? – luke cross Jan 17 '20 at 10:39
  • 1
    Inside the callback, you should notify the adapter. Check **[this](https://stackoverflow.com/questions/57127984/how-to-fix-my-arraylist-and-set-it-in-my-listview/57131519#57131519)** out. – Alex Mamo Jan 17 '20 at 10:41