我正在尝试读取我的云Firestore中的数据,并将其放入名为“ urlList”的数组中。 arrayList在onComplete中,如果我将数组记录到那里,它就可以工作(它充满了它应该具有的数据)。每当我在onComplete外部记录arrayList时,该数组为空。该问题似乎与onCompletelistener有关,但我无法找到问题所在/如何避免该问题。我添加了数字5和6的日志以检查问题所在,并且onCreate中的日志确实看到了5,但没有看到6

 public static ArrayList<String> urlList = new ArrayList<String>();
 public static ArrayList<String> groupList = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_middel_scherm);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.imageLayout);
    setSupportActionBar(toolbar);
    db = FirebaseFirestore.getInstance();
    mAuth = FirebaseAuth.getInstance();
    CurrentUser = FirebaseAuth.getInstance().getCurrentUser().getUid();
    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            if (firebaseAuth.getCurrentUser() == null) {
                startActivity(new Intent(MiddelScherm.this, LoginScherm.class));
            }
        }
    };
    ivImage = (ImageView) findViewById(R.id.ivImage);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SelectImage();

        }
    });

    databaseDownload();
    Log.d(TAG, "egregergegre");
    Log.d(TAG, String.valueOf(urlList));
    Log.d(TAG, "egregergegre");
    showEachImage(layout);
}

public void showEachImage(ConstraintLayout layout) {
    for (String element: urlList) {
        ImageView image = new ImageView(this);
        image.setLayoutParams(new android.view.ViewGroup.LayoutParams(80,60));
        image.setMaxHeight(20);
        image.setMaxWidth(20);
        Picasso.get().load(element).into(image);
        layout.addView(image);

    }
}

public void databaseDownload() {
    urlList.add("5");
    db.collection("user").document(CurrentUser).collection("meta-data")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        urlList.add("6");
                        for (DocumentSnapshot document : task.getResult()) {
                            String imageValue = document.getString("URL");
                            urlList.add(imageValue);
                            Log.d(TAG, String.valueOf(urlList));
                        }
                    } else {
                        Log.w(TAG, "Error getting documents.", task.getException());
                    }
                }
            });

    db.collection("user").document(CurrentUser).collection("Group")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (DocumentSnapshot document : task.getResult()) {
                            String groupValue = document.getString("Group");
                            groupList.add(groupValue);
                            Log.d(TAG, String.valueOf(groupList));
                        }
                    } else {
                        Log.w(TAG, "Error getting documents.", task.getException());
                    }
                }
            });
}


我什至尝试将公共无效的showEachImage(ConstraintLayout布局)放在onCompletelistener内,但在以下行中的“ this”处出现错误:ImageView image = new ImageView(this):所以我也放弃了此结构

    public void databaseDownload() {
urlList.add("5");
db.collection("user").document(CurrentUser).collection("meta-data")
        .get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    urlList.add("6");
                    for (DocumentSnapshot document : task.getResult()) {
                        String imageValue = document.getString("URL");
                        ImageView image = new ImageView(this);
                        image.setLayoutParams(new
                        android.view.ViewGroup.LayoutParams(80,60));
                        image.setMaxHeight(20);
                        image.setMaxWidth(20);
                        Picasso.get().load(imageValue).into(image);
                        layout.addView(image);
                        Log.d(TAG, String.valueOf(urlList));
                    }
                } else {
                    Log.w(TAG, "Error getting documents.", task.getException());
                }
            }
        });

db.collection("user").document(CurrentUser).collection("Group")
        .get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (DocumentSnapshot document : task.getResult()) {
                        String groupValue = document.getString("Group");
                        groupList.add(groupValue);
                        Log.d(TAG, String.valueOf(groupList));
                    }
                } else {
                    Log.w(TAG, "Error getting documents.", task.getException());
                }
            }
        });


}

最佳答案

添加OnCompleteListener的原因是get()启动了​​异步Task,查询完成后它将调用onComplete()方法。

get()方法立即返回,这意味着查询尚未完成,因此onCreate()方法将看不到任何数据。

任何需要数据的代码都应在onComplete()方法中执行。

10-08 01:40