简体   繁体   English

如何从 Firestore 文档将 Map 转换为 arrayList

[英]How to convert a Map into arrayList from firestore document

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.我的目标是查询一个集合并获取每个文档中包含三个或五个 hashmap 的文档。我的目标是获取具有不同 ID 的文档并填充到嵌套的回收器视图中。 But i facing trouble in converting the map values into arraylist但是我在将 map 值转换为 arraylist 时遇到了麻烦

My Main Model class:我的主要 Model class:

public class BattleGroupPosts {公共 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:从 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. 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);

我的数据库

在此处输入图像描述

It looks like you want to parse the map to get values, you can do this for multiple keys:看起来您想解析 map 以获取值,您可以对多个键执行此操作:

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将上面的图像 uri 添加到您的 arraylist

If you want to seperate on basis of ids:如果你想根据 id 分开:

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM