如何从 Firebase 实时数据库获取数据到 ListView?

-Quiz
  -MCQ
    -Question
       -1831
         -783444
           -//still got data
         -234567
           -//still got data

以上是我的 Firebase 实时数据库,我只想向我的783444和可点击的下一个活动显示和。234567ListView

请给一些建议来解决我的问题。非常感谢你。


梦里花落0921
浏览 78回答 1
1回答

皈依舞

要仅获取这些密钥,783444请234567在 aListView中使用以下代码行:DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();DatabaseReference ref = rootRef.child("Quiz").child("MCQ").child("Question").child("1831");ValueEventListener valueEventListener = new ValueEventListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onDataChange(DataSnapshot dataSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> list = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; for(DataSnapshot ds : dataSnapshot.getChildren()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String key = ds.getKey();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(key);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ListView listView = (ListView) findViewById(R.id.list_view);&nbsp; &nbsp; &nbsp; &nbsp; ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, list);&nbsp; &nbsp; &nbsp; &nbsp; listView.setAdapter(arrayAdapter);&nbsp; &nbsp; &nbsp; &nbsp; listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Go to next activity&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onCancelled(@NonNull DatabaseError databaseError) {}};ref.addListenerForSingleValueEvent(valueEventListener);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java