问题
My goal is to get query a collection and get the documents each document has three or five hashmap in it .my aim is to get the documents with different id's and populate in nested recycler view . But i facing trouble in converting the map values into arraylist
My Main Model class:
public class BattleGroupPosts {
private String userId;
public ArrayList<PostBattle> userPost;
public BattleGroupPosts() {
} ///below there getters and setters and constructions are there
My PostBattle Modelclass:
public class PostBattle {
private String id;
private String imageuri;
private String description;
private String publisher;
private String tags;
private String challenge_tittle;
private int NumberOfLikesPoints;
private int battle_post_count;
private long TimeLimit;
private Boolean post_time_done;
private @ServerTimestamp
Date timestamp;
public PostBattle() {
} //below the getter and setter and constructor
Querying from firestore:
private List<BattleGroupPosts> battleGroupPosts;
private ArrayList<PostBattle> battlePostLists;
private void GettingAllBattleGroupPosts(){
battleGroupPosts = new ArrayList<>();
battlePostLists = new ArrayList<>();
adapter = new BattlGroupAdapter(mContext,battleGroupPosts);//Setting the adapter
battle_posts.setAdapter(adapter);
CollectionReference GroupIdRef = mFirestore
.collection("OpenBattle")
.document(BattlesPost_creator)
.collection("BattlePost")
.document(BattlePost_id)
.collection("Participants");
GroupIdRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId() + " => " + document.getData());
final BattleGroupPosts groupPosts = document.toObject(BattleGroupPosts.class);
groupPosts.setUserId(groupPosts.getUserId());
groupPosts.setUserPosts(battlePostLists);
Map<String, PostBattle> batPost = (Map<String, PostBattle>) document.get("userPost1");
I get stuck here i don't know how to to add these to array list in the model class i tried converting hashMap to arraylist but my images getting populated with all items Id's in recylerview as it is in the for loop i guess. my method try:
List<PostBattle> newList = new ArrayList<PostBattle>(batPost.values());
Log.d(TAG, "onComplete: liisd:" + newList.get(0));
PostBattle postBattle = new PostBattle();
postBattle.setImageuri(String.valueOf(newList.get(1)));
postBattle.setNumberOfLikesPoints(Integer.valueOf(String.valueOf(newList.get(2))));
battlePostLists.add(postBattle);
battleGroupPosts.add(groupPosts);
adapter.notifyDataSetChanged(); ///These methods i did it inside for the loop
Have tried this :
PostBattle postBattle = new PostBattle();
Object value = batPost.get("imageuri");
postBattle.setImageuri(String.valueOf(value));
Log.d(TAG, "onComplete: value: " + value);
battlePostLists.add(postBattle);
groupPosts.setUserPosts(battlePostLists);
battleGroupPosts.add(groupPosts);
adapter.notifyDataSetChanged();
And this too:
Map<String, Object> idMaps = document.getData();
for (Map.Entry<String, Object> id : idMaps.entrySet()){
String userId = id.getKey();
Object yourObject = id.getValue();
Log.d(TAG, "onComplete: IDSSS: " + userId);
Log.d(TAG, "onComplete: Object: " + yourObject);
Map<String, PostBattle> batPost = (Map<String, PostBattle>) document.get("userPost1");
PostBattle postBattle = new PostBattle();
Object value = batPost.get("imageuri");
postBattle.setImageuri(String.valueOf(value));
batPost.clear();
battlePostLists.add(postBattle);
回答1:
It looks like you want to parse the map to get values, you can do this for multiple keys:
for (Map.Entry<String, PostBattle> params : batPost.entrySet()) {
if (params.getKey().equals("imageuri")) { // or something else
//add to your arraylist
}
}
or if you require only one:
String imageuri = batPost.getKey("imageuri").toString();
add the above image uri to your arraylist
If you want to seperate on basis of ids:
Map<String, yourobject> idmaps = document.getData();
for (Map.Entry<String, yourobject> id : idmaps.entrySet()) {
// do your code here
// first get the id
String userid = id.getKey();
//then do what you have done above
yourobject = id.getValue();
// do your logic on above object this will seperate your userids
}
来源:https://stackoverflow.com/questions/63295921/how-to-convert-a-map-into-arraylist-from-firestore-document