我正在创建一个包含“活动”类别的应用,该数据使用JSON显示。 在此,我使用Intent将ID字符串发送到另一个Activity 那么我只希望它显示包含cat_id = id(我发送到此活动)的数据
这是我的类别数组:-
[
{
"id": 1,
"name": "Udemy Courses"
},
{
"id": 2,
"name": "Hindi movies"
},
{
"id": 3,
"name": "Hollywood Hindi"
},
{
"id": 4,
"name": "Hollywood English"
},
{
"id": 5,
"name": "Netflix Series"
},
{
"id": 6,
"name": "Tv Series"
}
]
这是我的物品数组:-
[
{
"id": 1,
"title": "first title",
"description": "description description vdescription description description ",
"date": "2019-01-08",
"cat_id": 1
},
{
"id": 2,
"title": "second title",
"description": "vdescription description vdescription description description ",
"date": "2019-01-09",
"cat_id": 2
},
{
"id": 3,
"title": "title",
"description": "description description description description ",
"date": "2019-01-15",
"cat_id": 1
},
{
"id": 4,
"title": "title",
"description": "description description description ",
"date": "2019-01-09",
"cat_id": 1
}
]
我正在使用此代码
private int CategoryId;
CategoryId = Integer.parseInt(getIntent().getStringExtra("id"));
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObject = null;
for (int i=0; i < response.length();i++){
try {
jsonObject = response.getJSONObject(i);
if (jsonObject.getInt("cat_id")==CategoryId){
Item anime = new Item();
String Title = jsonObject.getString("title");
String Description = jsonObject.getString("description");
String Date = jsonObject.getString("date");
anime.setTitle(Title);
anime.setDescription(Description);
anime.setDate(Date);
listAnime.add(anime);
mProgress.dismiss();
} catch (JSONException e) {
e.printStackTrace();
}
}
setuprecyclerview(listAnime);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(request);
}
private void setuprecyclerview(List<Category> listAnime) {
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this,listAnime);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(myAdapter);
}
但这不会显示过滤的数据
例如:-当我单击id = 1(在类别数组中)的
然后它仅显示cat_id = 1的数据
但这显示了错误
Caused by: java.lang.NumberFormatException: Invalid int: "null"
答案 0 :(得分:0)
我为您发表了评论
@Override
public void onResponse (JSONArray response){
JSONObject jsonObject = null;
for (int i = 0; i < response.length(); i++) {
try {
jsonObject = response.getJSONObject(i);
//here you have all of the items and you should handle them
if (jsonObject.getInt("cat_id") == 1) {
//change 1 to the number you want
//here you have the jsonObject with specific id
//for example, I want to get the description from jsonObject
String Desc = jsonObject.getString("description);
}
Category anime = new Category();
anime.setName(jsonObject.getString("name"));
listAnime.add(anime);
mProgress.dismiss();
} catch (JSONException e) {
e.printStackTrace();
}
}
setuprecyclerview(listAnime);
}
},new Response.ErrorListener()
{
@Override
public void onErrorResponse (VolleyError error){
}
});
requestQueue =Volley.newRequestQueue(MainActivity .this);
requestQueue.add(request);
}
private void setuprecyclerview(List<Category> listAnime) {
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this, listAnime);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(myAdapter);
}