如何修复“ for循环”覆盖先前的数组值

时间:2019-04-17 13:01:59

标签: java android arrays listview

我正在使用查询从Firestore数据库中检索信息。从调试看来,当我将数据存储在ArrayList中时,每个值都会覆盖以前的值。所以只有最终值显示在我的ListView中。

for循环的各种变化。试图在大约5小时内找到解决方案。

public void addOrganisation(View view){
        db.collection("organisations").whereEqualTo("user", mAuth.getCurrentUser().getUid()).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()){

                    for (QueryDocumentSnapshot document : task.getResult()){

                        Log.i(TAG, document.getId() + " => " + document.get("organisation"));

                        ArrayList<String> myOrgs = new ArrayList<String>(asList(document.get("organisation").toString()));

                        Log.i("ARRAY LIST", myOrgs.toString());

                        ListView myOrgsListView = findViewById(R.id.myOrgsListView);

                        ArrayAdapter<String> orgArrayAdapter = new ArrayAdapter<String>(AppSettings.this, android.R.layout.simple_list_item_1, myOrgs);
                        myOrgsListView.setAdapter(orgArrayAdapter);
                    }
                } else {
                    Log.i(TAG, "Error getting documents:", task.getException());
                }
            }
        });

    }

日志:

2019-04-17 13:46:02.694 1404-1418/? D/hwcomposer: hw_composer sent 30 syncs in 123s
2019-04-17 13:46:15.402 32372-32385/? I/UsageStatsService: User[0] Flushing usage stats to disk
2019-04-17 13:46:17.150 32333-1938/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 99450911 , only wrote 99450720
2019-04-17 13:46:17.378 6108-6108/com.example.scrumpoker I/AppSettings: 6Euejc71xt7d2jvnB86Z => blahblah
2019-04-17 13:46:17.378 6108-6108/com.example.scrumpoker I/ARRAY LIST: [blahblah]
2019-04-17 13:46:17.380 6108-6108/com.example.scrumpoker I/AppSettings: 9RCJj725iVcpxKXMnOLo => blahblahblah
2019-04-17 13:46:17.380 6108-6108/com.example.scrumpoker I/ARRAY LIST: [blahblahblah]
2019-04-17 13:46:17.382 6108-6108/com.example.scrumpoker I/AppSettings: ZDzeIvTl5IRlcmcKsTfS => blahblahblahblah
2019-04-17 13:46:17.383 6108-6108/com.example.scrumpoker I/ARRAY LIST: [blahblahblahblah]
2019-04-17 13:46:17.383 6108-6108/com.example.scrumpoker I/AppSettings: y2Eiy2GmMTiW9rcwdL0u => stranger
2019-04-17 13:46:17.384 6108-6108/com.example.scrumpoker I/ARRAY LIST: [stranger]
2019-04-17 13:46:20.361 32333-5001/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 99757009 , only wrote 99604800
2019-04-17 13:46:31.306 31217-31233/? I/Finsky: [2039] hzb.run(3): Stats for Executor: BlockingExecutor ial@12fb4e9[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 96]

预期:ListView会填充查询返回的4个值

实际:ListView仅使用查询返回的最终值填充。

1 个答案:

答案 0 :(得分:1)

您的代码应如下所示:

 public void addOrganisation(View view){
       ListView myOrgsListView = findViewById(R.id.myOrgsListView);
        db.collection("organisations").whereEqualTo("user", mAuth.getCurrentUser().getUid()).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()){
                    ArrayList<String> myOrgs = new ArrayList<>();
                    for (QueryDocumentSnapshot document : task.getResult()){

                        Log.i(TAG, document.getId() + " => " + document.get("organisation"));

                        myOrgs.add(document.get("organisation").toString())

                        Log.i("ARRAY LIST", myOrgs.toString());

                    }
                     ArrayAdapter<String> orgArrayAdapter = new ArrayAdapter<String>(AppSettings.this, android.R.layout.simple_list_item_1, myOrgs);
                        myOrgsListView.setAdapter(orgArrayAdapter);
                } else {
                    Log.i(TAG, "Error getting documents:", task.getException());
                }
            }
        });

    }