需要有关将群组Firebase信息提供给ListView的帮助。我需要让所有的孩子都来自“商店”,但是不能。谁知道如何解决请帮助。虽然打印消息它给了我正确的数组,但我无法实现它到ListView。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dealer_list_view);
mListView = (ListView) findViewById(R.id.dealer_listView);
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
DataSnapshot friendsSnapshot = dataSnapshot.child("Shops");
for (DataSnapshot friendSnapshot : friendsSnapshot.getChildren()) {
String message = friendSnapshot.child("Dealer").getValue(String.class);
System.out.println(message);
DatabaseReference ref = FirebaseDatabase.getInstance().getReferenceFromUrl("My URl"+message);
FirebaseListAdapter<String> adapter = new FirebaseListAdapter<String>(DealerListView.this,
String.class,
android.R.layout.simple_list_item_1,
ref) {
@Override
protected void populateView(View v, String s, int i) {
TextView textView = (TextView) v.findViewById(android.R.id.text1);
textView.setText(s);
}
};
mListView.setAdapter(adapter);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
更新
- Shops
-AAA
Dealer: "AAA"
Email: "a@mail.ru"
SAP: "D666"
-BBB
Dealer: "BBB"
Email: "r@mail.ru"
SAP: "D333"
-CCC
Dealer: "BBB"
Email: "g@mail.ru"
SAP: "D222"
答案 0 :(得分:1)
看来你的问题太复杂了。这应该是您需要的唯一代码:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Shops");
FirebaseListAdapter<String> adapter = new FirebaseListAdapter<String>(
DealerListView.this,
String.class,
android.R.layout.simple_list_item_1,
ref) {
protected void populateView(View v, String s, int i) {
TextView textView = (TextView) v.findViewById(android.R.id.text1);
textView.setText(s);
}
protected String parseSnapshot(DataSnapshot snapshot) {
return snapshot.child("Dealer").getValue(String.class);
}
};
mListView.setAdapter(adapter);
因此,使用此代码,您可以获得商店快照列表,然后为每个快照提取Dealer
parseSnapshot
属性的值