【问题标题】:How to convert a Map into arrayList from firestore document如何将地图从firestore文档转换为arrayList
【发布时间】:2020-08-07 05:47:43
【问题描述】:

我的目标是查询一个集合并获取每个文档中包含三个或五个哈希图的文档。我的目标是获取具有不同 ID 的文档并填充到嵌套的回收器视图中。但我在将地图值转换为数组列表时遇到了麻烦

我的主要模型类:

公共类 BattleGroupPosts {

private String userId;
public ArrayList<PostBattle> userPost;

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

我的战后模型类:

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 查询:

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 循环中我猜. 我的方法试试:

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

【问题讨论】:

    标签: java android arraylist google-cloud-firestore


    【解决方案1】:

    看起来您想解析地图以获取值,您可以对多个键执行此操作:

    for (Map.Entry<String, PostBattle> params : batPost.entrySet()) {
         if (params.getKey().equals("imageuri")) { // or something else
    //add to your arraylist  
    
                                        }
    }
    

    或者如果您只需要一个:

    String imageuri = batPost.getKey("imageuri").toString();
    

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

    如果你想根据 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
    
    }
    

    【讨论】:

    • 感谢您的宝贵时间!但我不能使用 get 键来串行 String imageuri = batPost.getKey("imageuri").toString();
    • 这将基本上返回我正在转换为字符串的 PostBattle 对象
    • cloud firestore 输出就像地图中的地图一样,同时,您指定了 document.get("userPost1") ,那是列表中的最后一张地图
    • 我现在使用 Object 而不是 String 我可以获取图像 uri ...但由于某种原因,数组列表正在将第二个 id 图像 url 也添加到列表中...你能帮我吗这个谢谢!
    • 第二个id图片uri是什么意思
    猜你喜欢
    • 2020-12-04
    • 2021-08-24
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 2020-05-27
    • 2019-11-26
    相关资源
    最近更新 更多