【发布时间】: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