How to return a list coming from the database using AsyncTask, and then deliver to the class that will handle it?

1

I have an inner class that loads a list that comes from the database, but in return of doInBackground it returns the list empty.

I've already tested and for is getting right, then populate the list, but when it gets down there it just returns empty.

After the background is executed, I need it delivered to onPostExecute so that I can get the list as a result, ie there will be a method waiting for the list to be passed to the recycler.

public class CarregaLista extends AsyncTask<Void, String, List<DocumentSnapshot>> {

    ProgressDialog dialog;
    Context context;
    public final String TAG = "documento exist";

    public CarregaLista(Context ctx) {

        this.context = ctx;

    }

    @Override
    protected void onPreExecute() {

        dialog = new ProgressDialog(context);
        dialog.setMessage("carregando lista...");
        dialog.show();
    }

    @Override
    protected List<DocumentSnapshot> doInBackground(Void... voidss) {


            try {
                mRefFirestore = FirebaseFirestore.getInstance();
                mRefFirestore.collection("user")
                        .get()
                        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                            @Override
                            public void onComplete(@NonNull Task<QuerySnapshot> task) {

                                if (task.isSuccessful()) {

                                    List<DocumentSnapshot> snapshots
                                            = new ArrayList<DocumentSnapshot>();

                                    for (DocumentSnapshot snapshot : task.getResult()) {

                                        snapshots.add(snapshot);
                                    }

                                    documentSnapshot = snapshots;
                                }
                            }

                        });

            } catch (Exception e) {
                e.printStackTrace();
            }

            Log.d("LOG", "><<<<<<<<><<<<<<" + documentSnapshot.size());


        }

        return documentSnapshot;

    }

    @Override
    protected void onPostExecute(List<DocumentSnapshot> listaRecebida) {

        pegaLista(documentSnapshot);


        dialog.dismiss();
        Log.d("LOG", "><<<<<<<<><<<<<<" + documentSnapshot.size());
    }

}
    
asked by anonymous 23.06.2018 / 18:52

1 answer

0

You did not show the documentSnapshots statement. Do it like this:

private final List<DocumentSnapshot> documentSnapshots =
    new ArrayList<DocumentSnapshot>();

Next, on the line where you populate snapshots , populate documentSnapshots :

documentSnapshots.add(snapshot);
    
23.06.2018 / 19:20