I am writing multi level json parsing program, and able to get first level list, but not showing second level list while do tap on first level list item.
In short, i am listing categories just facing issue when trying to listing videos for tapped category.
I am doing something like this:
Main Activity [Listing Categories] > Another Activity [Listing Videos]
MainActivity.java:
static String NAME = "name";
static String THUMB = "thumb";
.............................
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("categories");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("name", jsonobject.getString("name"));
map.put("thumb", jsonobject.getString("thumb"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
MainAdapter.java:
// Capture ListView item click
itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Get the position
resultp = data.get(position);
Intent intent = new Intent(context, AnotherActivity.class);
// Pass name
intent.putExtra("name", resultp.get(MainActivity.NAME));
// Start AnotherActivity Class
context.startActivity(intent);
}
});
return itemView;
}
Note: using above code getting Categories Listing
AnotherActivity.java-
static String THUMB = "thumb";
static String TITLE = "title";
static String SUBTITLE = "subtitle";
static String DESCRIPTION = "description";
........................
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("videos");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
final JSONArray sources = jsonobject.getJSONArray("sources");
// Retrive JSON Objects
map.put("title", jsonobject.getString("title"));
map.put("thumb", jsonobject.getString("thumb"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
Manifest.xml:-
<activity android:name="com.example.zing.AnotherActivity" />
JSON:
{
"categories": [
{
"name": "Dev Events",
"thumb": "http://www.androidbegin.com/tutorial/flag/unitedstates.png",
"videos": [
{
"sources": [
"http://commondatastorage.googleapis.com/gtv_template_assets/IO2010-Keynote-day1.mp4"
],
"thumb": "http://www.androidbegin.com/tutorial/flag/unitedstates.png",
"title": "2010 Day 1 Keynote",
"subtitle": "Dev Events",
"description": "IO2010 Keynote"
},
{
"sources": [
"http://commondatastorage.googleapis.com/gtv_template_assets/IO2010-Keynote-day2-android.mp4"
],
"thumb": "http://www.androidbegin.com/tutorial/flag/russia.png",
"title": "2010 Day 2 Keynote",
"subtitle": "Dev Events",
"description": "IO2010 Keynote Android"
}
]
},
{
"name": "Technology",
"thumb": "http://www.androidbegin.com/tutorial/flag/russia.png",
"videos": [
{
"sources": [
"http://commondatastorage.googleapis.com/gtv_template_assets/CWS-HowTo.mp4"
],
"thumb": "http://www.androidbegin.com/tutorial/flag/russia.png",
"title": "Uploading your App",
"subtitle": "Technology",
"description": "CWS HowTo"
},
{
"sources": [
"http://commondatastorage.googleapis.com/gtv_template_assets/CWS-GettingStarted.mp4"
],
"thumb": "http://www.androidbegin.com/tutorial/flag/unitedstates.png",
"title": "Getting Started with Apps for the Chrome Web Store",
"subtitle": "Technology",
"description": "Arne Roomann-Kurrik"
}
]
}
]
}