你好,我是Firebase的新手,你能帮我把孩子的名字从我的Firebase转到列表视图吗?
这是我的数据库 这是我的代码,但是不起作用
公共类教育扩展了AppCompatActivity {
private DatabaseReference fNotesDatabase;
ListView listView;
ArrayList<String> arraylist = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_education);
Firebase.setAndroidContext(this);
fNotesDatabase = FirebaseDatabase.getInstance().getReference().child("Notes").child("education").child();
listView = (ListView) findViewById(R.id.listview);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arraylist);
listView.setAdapter(arrayAdapter);
fNotesDatabase.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Map<String, Object> map = new HashMap<String, Object>();
arraylist.add(map.values().toString());
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
Map<String, Object> map = new HashMap<String, Object>();
arraylist.add(map.values().toString());
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
答案 0 :(得分:0)
如果要检索Notes
下的直接子级,请尝试以下操作:
ArrayAdapter adapter;
ListView listView;
ArrayList<String> arraylist = new ArrayList<>();
listView = findViewById(R.id.listview);
fNotesDatabase = FirebaseDatabase.getInstance().getReference().child("Notes");
fNotesDatabase.addSingleValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String values = datas.getKey();
arraylist.add(values);
adapter = new ArrayAdapter(Education.this, android.R.layout.simple_list_item_single_choice, arraylist);
listView.setAdapter(adapter);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
首先在子项Notes
上添加引用,然后在其内部循环并使用keys
检索getKey()