I have this json data
{
"Categories": [
{
"description": "",
"id": 33,
"name": "News",
"parent_id": 0,
"position": 3,
"published_videos": 953,
"tag": "news",
"total_videos": 953
},
{
"description": "",
"id": 34,
"name": "Health",
"parent_id": 0,
"position": 22,
"published_videos": 17,
"tag": "tena",
"total_videos": 17
},
{
"description": "",
"id": 35,
"name": "Interview",
"parent_id": 0,
"position": 23,
"published_videos": 271,
"tag": "qalemeteyeq",
"total_videos": 271
},
{
"description": "",
"id": 36,
"name": "Trailers",
"parent_id": 0,
"position": 21,
"published_videos": 72,
"tag": "movie",
"total_videos": 72
},
{
"description": "",
"id": 37,
"name": "Technology",
"parent_id": 0,
"position": 24,
"published_videos": 17,
"tag": "technology",
"total_videos": 17
},
"description": "",
"id": 39,
"name": "International News",
"parent_id": 33,
"position": 1,
"published_videos": 319,
"tag": "international_news",
"total_videos": 319
},
]
}
And I parsed this json file and added it to a navigation drawer. On the navigation drawer all I have is the name of each categories. When a user clicked on the categories there is another json that I wanted to parse which lists the items in a recycler view. For example I am using like this: http://mydomain/category/id/videos I want to replace the id with the one that is clicked(from the json) and get the list of items from the Internet. But I don't know how to get the id and pass it through an intent. Is there some kind of design guideline I should use to handle such kind of issues?
private String[] getCategoriesJson(String categoriesJsonStr)
throws JSONException {
JSONObject categoriesJSON = new JSONObject(categoriesJsonStr);
JSONArray categoriesArray = categoriesJSON.getJSONArray("Categories");
String[] resultStrs = new String[categoriesArray.length()+1];
resultStrs[0] = "Home";
for (int i=0; i< categoriesArray.length(); i++){
JSONObject category = categoriesArray.getJSONObject(i);
String name = category.getString("name");
Integer categoryId = category.getInt("id");
//Integer parentId = category.getInt("parent_id");
resultStrs[i+1] = name;
}
for (String s: resultStrs){
Log.v(LOG_TAG, "Category entry: " + s);
}
return resultStrs;
}
@Override
protected String[] doInBackground(String... params) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String categoriesJsonstr = null;
try {
URL url = new URL("http://mydomain/categories/");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while((line = reader.readLine()) != null){
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
return null;
}
categoriesJsonstr = buffer.toString();
//Log.v(LOG_TAG, "Categories JSON String: " + categoriesJsonstr);
}catch (IOException e) {
Log.e(LOG_TAG, "Error", e);
return null;
}finally {
if (urlConnection != null){
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
}catch (final IOException e){
Log.e(LOG_TAG, "Error closing stream", e);
}
}
}
try {
return getCategoriesJson(categoriesJsonstr);
}catch (JSONException e){
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
}
return null;
}
The problem is I can get the name of the category using findViewById. But where shall I store the id of each categories?