如何从Firestore文档将Map转换为arrayList [英] How to convert a Map into arrayList from firestore document

查看:108
本文介绍了如何从Firestore文档将Map转换为arrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是查询集合并获取每个文档中包含三个或五个哈希图的文档.我的目标是获取具有不同ID的文档并在嵌套的回收者视图中进行填充.但是我在将映射值转换为arraylist时遇到了麻烦

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

我的主要模型班:

公共类BattleGroupPosts {

public class BattleGroupPosts {

private String userId;
public ArrayList<PostBattle> userPost;

public BattleGroupPosts() {
} ///below there getters and setters and constructions are there

我的PostBattle模型类:

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

从Firestore查询:

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"); 

我在这里卡住了,我不知道如何将这些添加到模型类的数组列表中,我尝试将hashMap转换为arraylist,但是我的图像填充了recylerview中所有ID,因为它在for循环中. 我的方法尝试:

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  

尝试过:

                    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();

这也是:

    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:

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();

将上述图像uri添加到您的数组列表中

add the above image uri to your arraylist

如果要基于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

}

这篇关于如何从Firestore文档将Map转换为arrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆