【发布时间】:2019-03-03 14:20:23
【问题描述】:
收到这个 Json 响应
{
"result": "success",
"data": [
{
"comment": "hi",
"addedon": "2019-03-01 09:31:28",
"user_id": "1",
"id": "22",
"name": "chitransh",
"photo": "user.png"
},
{
"comment": "hixdh ",
"addedon": "2019-03-01 09:31:32",
"user_id": "1",
"id": "23",
"name": "chitransh",
"photo": "user.png"
},
{
"comment": "xy uf",
"addedon": "2019-03-01 09:31:36",
"user_id": "1",
"id": "24",
"name": "chitransh",
"photo": "user.png"
},
{
"comment": "cgui ",
"addedon": "2019-03-01 09:31:39",
"user_id": "1",
"id": "25",
"name": "chitransh",
"photo": "user.png"
},
{
"comment": "hi",
"addedon": "2019-03-01 09:33:32",
"user_id": "1",
"id": "26",
"name": "chitransh",
"photo": "user.png"
},
{
"comment": "fg ",
"addedon": "2019-03-01 09:33:34",
"user_id": "1",
"id": "27",
"name": "chitransh",
"photo": "user.png"
},
{
"comment": "chkk hhh",
"addedon": "2019-03-01 09:33:39",
"user_id": "1",
"id": "28",
"name": "chitransh",
"photo": "user.png"
},
{
"comment": "yubvcg ",
"addedon": "2019-03-01 09:33:43",
"user_id": "1",
"id": "29",
"name": "chitransh",
"photo": "user.png"
},
{
"comment": "ggi ",
"addedon": "2019-03-01 09:33:47",
"user_id": "1",
"id": "30",
"name": "chitransh",
"photo": "user.png"
},
{
"comment": "cfg hj",
"addedon": "2019-03-01 09:33:50",
"user_id": "1",
"id": "31",
"name": "chitransh",
"photo": "user.png"
},
{
"comment": "vbj",
"addedon": "2019-03-01 09:33:53",
"user_id": "1",
"id": "32",
"name": "chitransh",
"photo": "user.png"
},
{
"comment": "hehh",
"addedon": "2019-03-01 11:32:13",
"user_id": "18",
"id": "33",
"name": "Test User 3",
"photo": "avatar10.png"
},
{
"comment": "hhh",
"addedon": "2019-03-01 11:43:26",
"user_id": "18",
"id": "37",
"name": "Test User 3",
"photo": "avatar10.png"
},
{
"comment": "hahahah",
"addedon": "2019-03-01 12:04:14",
"user_id": "18",
"id": "42",
"name": "Test User 3",
"photo": "avatar10.png"
},
{
"comment": "hii",
"addedon": "2019-03-01 12:16:05",
"user_id": "18",
"id": "43",
"name": "Test User 3",
"photo": "avatar10.png"
},
{
"comment": "hou",
"addedon": "2019-03-01 12:16:12",
"user_id": "18",
"id": "44",
"name": "Test User 3",
"photo": "avatar10.png"
},
{
"comment": "hi",
"addedon": "2019-03-01 12:16:28",
"user_id": "18",
"id": "45",
"name": "Test User 3",
"photo": "avatar10.png"
},
{
"comment": "hi",
"addedon": "2019-03-01 12:16:45",
"user_id": "18",
"id": "46",
"name": "Test User 3",
"photo": "avatar10.png"
},
{
"comment": "hi",
"addedon": "2019-03-01 12:16:52",
"user_id": "18",
"id": "47",
"name": "Test User 3",
"photo": "avatar10.png"
},
{
"comment": "gg",
"addedon": "2019-03-01 12:17:09",
"user_id": "18",
"id": "48",
"name": "Test User 3",
"photo": "avatar10.png"
},
{
"comment": "heyy",
"addedon": "2019-03-01 12:31:26",
"user_id": "1",
"id": "49",
"name": "chitransh",
"photo": "user.png"
},
{
"comment": "fff",
"addedon": "2019-03-01 12:36:52",
"user_id": "1",
"id": "52",
"name": "chitransh",
"photo": "user.png"
},
{
"comment": "hhh",
"addedon": "2019-03-02 00:13:41",
"user_id": "18",
"id": "54",
"name": "Test User 3",
"photo": "avatar10.png"
},
{
"comment": "jii",
"addedon": "2019-03-02 02:33:55",
"user_id": "1",
"id": "61",
"name": "chitransh",
"photo": "user.png"
},
{
"comment": "dsdsdsdsd",
"addedon": "2019-03-03 03:44:33",
"user_id": "1",
"id": "63",
"name": "chitransh",
"photo": "user.png"
}
],
"reply": {
"22": [
{
"comment": "hahaha",
"addedon": "2019-03-02 00:34:50",
"user_id": "18",
"id": "58",
"name": "Test User 3",
"photo": "avatar10.png"
},
{
"comment": "@chitransh heyyy",
"addedon": "2019-03-03 03:44:59",
"user_id": "1",
"id": "65",
"name": "chitransh",
"photo": "user.png"
},
{
"comment": "@chitransh heyy test user",
"addedon": "2019-03-03 03:45:23",
"user_id": "1",
"id": "66",
"name": "chitransh",
"photo": "user.png"
},
{
"comment": "@chitransh hiw are you",
"addedon": "2019-03-03 04:06:09",
"user_id": "1",
"id": "67",
"name": "chitransh",
"photo": "user.png"
}
],
"23": [
{
"comment": "@chitransh hhhh",
"addedon": "2019-03-02 01:04:58",
"user_id": "18",
"id": "59",
"name": "Test User 3",
"photo": "avatar10.png"
}
],
"24": [],
"25": [],
"26": [],
"27": [],
"28": [],
"29": [],
"30": [],
"31": [
{
"comment": "@chitranshdsdsdsdsd",
"addedon": "2019-03-02 05:07:44",
"user_id": "1",
"id": "62",
"name": "chitransh",
"photo": "user.png"
}
],
"32": [],
"33": [],
"37": [],
"42": [],
"43": [],
"44": [],
"45": [],
"46": [],
"47": [],
"48": [],
"49": [
{
"comment": "chitransh hi",
"addedon": "2019-03-01 12:32:47",
"user_id": "1",
"id": "50",
"name": "chitransh",
"photo": "user.png"
},
{
"comment": "chitransh sdff",
"addedon": "2019-03-01 12:33:00",
"user_id": "1",
"id": "51",
"name": "chitransh",
"photo": "user.png"
},
{
"comment": "chitransh hi",
"addedon": "2019-03-01 12:42:59",
"user_id": "1",
"id": "53",
"name": "chitransh",
"photo": "user.png"
}
],
"52": [],
"54": [
{
"comment": "@Test User 3kjj",
"addedon": "2019-03-02 01:20:55",
"user_id": "18",
"id": "60",
"name": "Test User 3",
"photo": "avatar10.png"
}
],
"61": [
{
"comment": "@chitranshdsdsdsdsdsd",
"addedon": "2019-03-03 03:44:40",
"user_id": "1",
"id": "64",
"name": "chitransh",
"photo": "user.png"
}
],
"63": []
}
}
已完成数据 JSONArray ,现在正在处理回复 正如您在回复 JSONArray 中看到的那样,我将从数据中获取 id,因此我将该 id 作为键 ex 存储在我的 Map 中。
Map<String,Arraylist<ReplyCommentModel>>
所以 String 指的是那个 id 。
现在在 RecyclerView 项目中的 buttonOnClick 我正在使用此代码-->
viewRepliesButton.setOnClickListener(v -> {
if (collpased){
collpased = false;
replyRecyclerView.setVisibility(View.VISIBLE);
GroupAdapter<ViewHolder> adapter = new GroupAdapter<>();
replyRecyclerView.setAdapter(adapter);
replyRecyclerView.setLayoutManager(new LinearLayoutManager(context));
replyCommentModels = replyModelsMap.get(String.valueOf(model.comment_id));
if(replyCommentModels != null){
Log.e("replComments ","Size" +replyCommentModels.size());
for(ReplyCommentModel replyCommentModel : replyCommentModels){
adapter.add(new ReplyCommentItem(replyCommentModel,context));
}
}
viewRepliesButton.setText("hide");
} else {
collpased = true;
replyRecyclerView.setVisibility(View.GONE);
viewRepliesButton.setText("Tap to view reply");
}
});
但地图中的所有项目都是可见的在 recyclerView 中我的代码有问题吗? 由于我从我的适配器项模型传递注释 id 的值是来自 JSON 响应的数据键,您可以在上面看到 现在我单击列表并将其 id 传递给 map 以获取该特定 id 的数组列表,也许 map 用于执行此操作,或者有其他更好的方法来执行此操作? 新使用 Map 和 Arraylist tbh ,任何适当的指导将不胜感激
我正在像这样保存我的地图
private Map<String,ArrayList<ReplyCommentModel>> replyModelMap = new HashMap<>();
try {
JSONObject obj = new JSONObject(response);
String status = obj.getString("result");
if (status.equals("success")) {
JSONArray jsonArray = obj.getJSONArray("data");
if(model.size() == 0){
fetchComments(jsonArray);
JSONObject replyObject = obj.getJSONObject("reply");
for(int i=0;i<model.size();i++){
JSONArray replyArrays = replyObject.getJSONArray(String.valueOf(model.get(i).comment_id));
for(int j=0;j<replyArrays.length();j++){
JSONObject jsonObject1 = replyArrays.getJSONObject(j);
Integer id = jsonObject1.getInt("id");
String name = jsonObject1.getString("name");
String comment_image = jsonObject1.getString("photo");
Integer user_id = jsonObject1.getInt("user_id");
String comment = jsonObject1.getString("comment");
String added_on = jsonObject1.getString("addedon");
String imageURl = Constant.IMAGE_URL+Constant.COMMENT_USER_IMAGE_URL+comment_image;
replyModel.add(new ReplyCommentModel(comment,name,imageURl,added_on,user_id,id));
}
if(replyModel != null){
replyModelMap.put(String.valueOf(model.get(i).comment_id),replyModel);
}
}
Log.e("ReplyModelMapSize","----> "+replyModelMap.size());
setUpRecyclerview();
}else{
model.clear();
fetchComments(jsonArray);
upDateRecyclerView();
}
}
}catch (Exception e){
e.printStackTrace();
}
【问题讨论】: