如何在 JSON 的 Recycler 视图中显示特定类别
Posted
技术标签:
【中文标题】如何在 JSON 的 Recycler 视图中显示特定类别【英文标题】:How do I show specific category in Recycler view from JSON 【发布时间】:2021-11-22 09:28:59 【问题描述】:我想将项目数据添加到仅特定类别的 recyclerview。 例如,在我的 JSON 中,您可以看到 cab 类别中有 2 个项目,所以我只想添加这 2 个项目,反之亦然,如果类别等于“卡车”,那么我只想添加所有卡车类别项目。
这是我的 JAVA
@Override
public void onResponse(String response)
try
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++)
JSONObject jsonObject = jsonArray.getJSONObject(i);
String category = jsonObject.getString("category_name");
////// /* stuckk here??? if (category.contains("cabs"))*/ /////
JSONArray JsonArray = jsonObject.getJSONArray("projects");
for (int ii = 0; ii < JsonArray.length(); ii++)
JSONObject jObject = JsonArray.getJSONObject(ii);
p_id = jObject.getString("id");
p_name = jObject.getString("name");
ProjectItemModel projectItemModel = new ProjectItemModel(p_id, p_name);
ProjectList.add(projectItemModel);
mAdapter = new ProjectAdapter(ProjectList);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context);
project_rv.setLayoutManager(layoutManager);
project_rv.setAdapter(mAdapter);
这是我的 JSON API 调用
[
"category_name": "Cabs",
"category_image": "cab.png",
"projects": [
"id": "1002",
"uid": "SP1001",
"name": "car1"
]
,
"category_name": "Cabs",
"category_image": "cab.png",
"projects": [
"id": "1003",
"uid": "SP1003",
"name": "car2"
]
,
"category_name": "Truck",
"category_image": "truck.png",
"projects": [
"id": "1001",
"uid": "SP1001",
"name": "Truck1"
]
]
请帮我解决这个问题谢谢
【问题讨论】:
【参考方案1】:您的问题在于列出项目
ArrayList<String> categories_to_display=new ArrayList();
public void onResponse(String response)
ArrayList<ProjectItemModel> ProjectList = new ArrayList<ProjectItemModel>();
try
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++)
JSONObject jsonObject = jsonArray.getJSONObject(i);
String category = jsonObject.getString("category_name");
////// /* stuckk here??? if (category.contains("cabs"))*/ /////
// if (category.equalsIgnoreCase("carbs")) //you could use this for only one category
if (categories_to_display.contains(category))
JSONArray JsonArray = jsonObject.getJSONArray("projects");
for (int ii = 0; ii < JsonArray.length(); ii++)
JSONObject jObject = JsonArray.getJSONObject(ii);
// p_id = jObject.getString("id");
// p_name = jObject.getString("name");
ProjectItemModel projectItemModel = new ProjectItemModel(jObject.getString("id"), jObject.getString("name"));
ProjectList.add(projectItemModel);
mAdapter = new ProjectAdapter(ProjectList);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context);
project_rv.setLayoutManager(layoutManager);
project_rv.setAdapter(mAdapter);
所以现在只需将您需要的类别添加到 arraylist categories_to_display 然后清除数组,如果您需要设置新类别来检查或者如果它是单个类别,只需使用此行而不是其下方的行,然后注释下面的行它并取消注释这个// if (category.equalsIgnoreCase("carbs")) //you could use this for only one category
【讨论】:
以上是关于如何在 JSON 的 Recycler 视图中显示特定类别的主要内容,如果未能解决你的问题,请参考以下文章
在Recycler视图上添加图像视图时,Android导航抽屉动画滞后/减速
Recycler视图在android studio中未显示一张卡片视图
Android - 为啥 Recycler 视图未显示在设备上? .我认为我的代码是正确的,但视图没有显示