arrayList仅在onCompletelistener内部更新,而不在其外部更新

时间:2018-08-27 19:56:59

标签: java android arraylist scope

我正在尝试读取我的云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 layout)放在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());
                }
            }
        });

}

2 个答案:

答案 0 :(得分:0)

我认为与ImageView发生暗恋的原因是“ this”是onComplete方法中的OnCompleteListener类。

答案 1 :(得分:0)

您添加OnCompleteListener的原因是get()启动了异步 Task,当查询完成。

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

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