Reputation: 2725
I am getting list of data from firebase database using addValueEventListener, I got the data in dataSnapshot and I can call getValue to get the data, but is there any way I can directly convert it to Arraylist Right now I am doing like this
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot matchSnapShot: dataSnapshot.getChildren()) {
Match match = matchSnapShot.getValue(Match.class);
matchList.add(match);
}
Object obj= dataSnapshot.getValue();
Toast.makeText(NewMatchActivity.this,"got data",Toast.LENGTH_LONG).show();
}
but I want some thing like this
ArrayList<Match> matchList = dataSnapshot.getValue(ArrayList<Match.class>);
Upvotes: 2
Views: 1256
Reputation: 2962
Try this:
GenericTypeIndicator<List<Match>> t = new GenericTypeIndicator<List<Match>>(){};
List<Match> matchList = dataSnapshot.getValue(t);
Upvotes: 1
Reputation: 598728
It depends a bit on your exact data structure (a representable snippet of the JSON as text would help for that), but most likely the data you are getting back is a Map and not a List.
public void onDataChange(DataSnapshot dataSnapshot) {
Map<String,Match> map = (Map<String, Match>) dataSnapshot.getValue();
Collection<Match> matches = map.values();
}
Upvotes: 1