插入多个项目后,为什么数组列表ratingItemList显示为空? [英] Why is the array list ratingItemList showing as empty after insertion of several items?

查看:28
本文介绍了插入多个项目后,为什么数组列表ratingItemList显示为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文档快照循环中,将评分添加到ratingItemList中.我肯定知道这一点,因为我还在打印日志中列表的大小,而且它还在不断增加.但是,在退出该循环后,只是要确保我检查它是否为空,并在日志中显示空列表".你们可以帮我找出错误的地方吗?

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());
        }
    }

}

推荐答案

如果在第一行中调用后台线程结果并在下一行中打印,则您的回调方法不能保证在下一行之前运行.它将开始执行第一行的线程,而无需等待响应运行下一行.所以你把它弄空了.

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.

for 循环之后,也可以在回调 onSuccess()方法中检查列表大小:

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();
            }
        }
    });

}

}

这篇关于插入多个项目后,为什么数组列表ratingItemList显示为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆