How to return an avinda list of the database using Asynctask, and then deliver it to the class that will manipulate it?

Asked

Viewed 89 times

1

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

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

After the background run, I need it delivered to the onPostExecute so I can get the list as a result, IE, there will be a method waiting for the list and pass 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());
    }

}
  • Exchange for pegaLista(listaRecebida);

  • sorry didn’t understand

  • ah ok,then already I did not worked too,this is the problem the list arrives empty

  • So I don’t know, I’m going to owe. It would be nice to post the full code as suggested by the author of the reply.

1 answer

0

You didn’t show the statement of documentSnapshots. Do it like this:

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

Then on the line where you populate snapshots, populy documentSnapshots:

documentSnapshots.add(snapshot);
  • Good morning Remember I made the statement in the foreign class,I declare it that way that you passed where ? within the class Inner ?

  • Where it is declared today, exchange for the suggestion.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.