Why is the array list ratingItemList showing as empty after insertion of several items?

心已入冬 提交于 2021-02-11 17:45:26

问题


While its in the Document Snapshot loop its adding the ratings to the ratingItemList. I know this for sure because I'm also printing the size of the list in the Log and it's increasing. But after it comes out of that loop just to be sure I check whether it is empty or not and it prints Empty List in the log. Can you guys help me out locating the error?

package com.example.ratingapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.ArrayList;
import java.util.List;

public class YourRating extends AppCompatActivity {
private List<ratingItem> ratingItemList;
private FirebaseFirestore db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_your_rating);

        ratingItemList=new ArrayList<>();



        db = FirebaseFirestore.getInstance();
        db.collection("userRatings").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                if(!queryDocumentSnapshots.isEmpty()){
                    Log.d("Check1","Ratings Exist");
                    List<DocumentSnapshot> documentSnapshots = queryDocumentSnapshots.getDocuments();
                    for(DocumentSnapshot doc : documentSnapshots) {
                        String rating = doc.getString("Rating");
                        //Log.d("Rating",rating);
                        com.google.firebase.Timestamp date = doc.getTimestamp("Date");
                        //Log.d("Date",date.toString());
                        ratingItem newRatingItem = new ratingItem(rating, date);
                        Log.d("Rating", newRatingItem.getRating());
                        Log.d("Date", newRatingItem.getTimestamp().toString());


                        ratingItemList.add(newRatingItem);
                        Log.d("Size ",String.valueOf(ratingItemList.size()));

                    }
                }
                else{
                    Toast.makeText(YourRating.this,"No ratings available!",Toast.LENGTH_LONG).show();
                }
            }
        });


        if(ratingItemList.isEmpty()){
            Log.d("Empty","Empty List");
        }
        for(ratingItem r: ratingItemList){
            Log.d("Rating1",r.getRating());
            Log.d("Date1",r.getTimestamp().toString());
        }
    }

}

回答1:


if you call for background thread result in first line and print it on very next line, your callback method does not give guarantee to run before the very next line. It will start to execute thread of first line and without waiting for response run the next line. So you are getting it empty.

Check list size also in callback onSuccess() method, after for loop:

public class YourRating extends AppCompatActivity {
private List<ratingItem> ratingItemList;
private FirebaseFirestore db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_your_rating);

    ratingItemList = new ArrayList<>();


    db = FirebaseFirestore.getInstance();
    db.collection("userRatings").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
        @Override
        public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
            if (!queryDocumentSnapshots.isEmpty()) {
                Log.d("Check1", "Ratings Exist");
                List<DocumentSnapshot> documentSnapshots = queryDocumentSnapshots.getDocuments();
                for (DocumentSnapshot doc : documentSnapshots) {
                    String rating = doc.getString("Rating");
                    //Log.d("Rating",rating);
                    com.google.firebase.Timestamp date = doc.getTimestamp("Date");
                    //Log.d("Date",date.toString());
                    ratingItem newRatingItem = new ratingItem(rating, date);
                    Log.d("Rating", newRatingItem.getRating());
                    Log.d("Date", newRatingItem.getTimestamp().toString());


                    ratingItemList.add(newRatingItem);
                    Log.d("Size ", String.valueOf(ratingItemList.size()));

                }
                if (ratingItemList.isEmpty()) {
                    Log.d("Empty", "Empty List");
                }
                for (ratingItem r : ratingItemList) {
                    Log.d("Rating1", r.getRating());
                    Log.d("Date1", r.getTimestamp().toString());
                }
            } else {
                Toast.makeText(YourRating.this, "No ratings available!", Toast.LENGTH_LONG).show();
            }
        }
    });

}

}



回答2:


you success listener runs on the background thread and it is a promise that will be run when the data will be obtained from the firebase. on the other hand the piece of code where you check the array list is empty or not runs on the ui thread. It does not wait for the data to be fetched.



来源:https://stackoverflow.com/questions/61080499/why-is-the-array-list-ratingitemlist-showing-as-empty-after-insertion-of-several

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!