【发布时间】:2019-04-17 13:01:58
【问题描述】:
我正在使用查询从我的 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 仅填充查询返回的最终值。
【问题讨论】:
-
ArrayList 初始化必须在 for 循环之外。每次在 for 循环中创建新的 ArrayList 时,您只能看到最后一个输出。
标签: java android arrays listview