我正在点击一个返回JSON数据的外部API(新的dvd标题).我能够解析出JSON并将每个DVD标题和其他DVD信息列入ListView就好了.我还能够为原始数据(标题字符串)设置一个onListItemClick方法.我最终为onListItemClick方法写了类似的东西:
需要注意的是,productArray是一个类var,由另一个包含JSONObject数组的方法设置.
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(DvdListingActivity.this, MovieProductActivity.class);
try {
JSONObject jsonObj = productArray.getJSONObject(position);
i.putExtra("mTitle", jsonObj.getJSONObject("Title").opt("val").toString());
i.putExtra("mRelDate", jsonObj.getJSONObject("RelDate").opt("val").toString());
i.putExtra("mDesc", jsonObj.getJSONObject("Desc").opt("val").toString());
i.putExtra("mRating", jsonObj.getJSONObject("MPAA").getJSONObject("Rating").opt("val").toString());
i.putExtra("mActors", jsonObj.getJSONObject("Actors").opt("val").toString());
i.putExtra("mImage", jsonObj.getJSONObject("Image").opt("val").toString());
startActivity(i);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码都有效,但我认为GOTTA是一种更好的方式让我将数据传递给另一个Activity.我以为我能够传递一个包含dvd影片所有数据的JSONObject,而不是单独设置每个数据点.
我尝试了一个半星期来弄清楚如何使用Parcelable.我尝试实例化一个新的JSONObject jsonObj,它实现了Parcelable而没有运气.我一直在我的LogCat中收到错误,该错误表示该对象是不可分割的.
我已经尝试过阅读Android开发者网站和其他博客,但我无法将他们的示例应用到我需要做的事情上.
任何帮助将非常感激