我从从Firestore数据库检索的地图中列出了值列表。问题是它不会出现在我的listView
上。我从另一个地图启动了另一个列表,该列表显示在我的listView
中。为什么从Firestore文档中检索的地图中的列表无法正常工作,而其他列表却可以正常工作?
public class RecipeActivity extends Activity {
private ListView listView;
private ArrayAdapter<Object> arrayAdapter;
private FirebaseFirestore newRecipeDb;
private Map<String, Object> meMap = new HashMap<String, Object>();
private List<Object> list = new ArrayList<>();
private List<String> listStrings = new ArrayList<> ();
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe);
textView = findViewById(R.id. breadRecipe);
listView = findViewById(R.id.listIngredient);
arrayAdapter = new ArrayAdapter<Object>(this,
android.R.layout.simple_list_item_activated_1, list);
newRecipeDb = FirebaseFirestore.getInstance();
newRecipeDb.collection("users")
.document("bakeryMgt")
.collection("New Recipe")
.document("cherri pie")
.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
System.out.println("DocumentSnapshot data " + document.getData());
meMap = document.getData();
list = new ArrayList<Object>(meMap.values());
listView.setAdapter(arrayAdapter);
} else {
System.out.println("No such document");
}
} else {
System.out.println("get failed with " + task.getException());
}
}
});
}
}
答案 0 :(得分:0)
在您的SimpleJdbcCall getQACall = simpleJdbcCallFactory.create(jdbcTemplate)
.withSchemaName(SCHEMA_ARTICLE)
.withProcedureName(SP_GET_ALL_QUESTIONS_ANSWERS)
.withoutProcedureColumnMetaDataAccess()
.declareParameters(
// etc
中,您将基于onCreate
创建一个ArrayAdapter
:
list
然后在arrayAdapter = new ArrayAdapter<Object>(this,
android.R.layout.simple_list_item_activated_1, list);
内,创建一个新列表:
onComplete
这意味着 list = new ArrayList<Object>(meMap.values());
并不指向您将数据放入的arrayAdapter
。
解决方案是将文档中的数据添加到现有的list
:
list
在同时初始化两者时,我还建议将适配器绑定到列表视图:
list.add(meMap.values());
然后在将项目添加到listView = findViewById(R.id.listIngredient);
arrayAdapter = new ArrayAdapter<Object>(this,
android.R.layout.simple_list_item_activated_1, list);
listView.setAdapter(arrayAdapter);
时仅通知适配器其数据已更改:
list