【发布时间】:2014-02-25 06:34:35
【问题描述】:
我正在编写多级json解析程序,可以得到一级列表,但在点击一级列表项时不显示二级列表。
简而言之,我列出的类别只是在尝试为点击类别列出视频时遇到问题。
我正在做这样的事情:
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;
}
注意:使用上面的代码获取类别列表
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"
}
]
}
]
}
【问题讨论】:
-
您的解析看起来不错。你在说什么第二层
-
@Raghunandan 列出第二级视频
-
你需要遍历分类jsonarray。那部分代码在哪里
-
获取类别未获取类别下的视频
-
这就是我所说的。类别是一个 json 数组。如果您希望所有数据循环通过它