将 JSONObject 传递给另一个活动 [英] Passing JSONObject into another activity

查看:33
本文介绍了将 JSONObject 传递给另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个返回 JSON 数据(新 DVD 标题)的外部 API.我能够解析出 JSON 并将每个 dvd 标题和其他 dvd 信息列出到 ListView 中.我还能够为原始数据(标题字符串)设置一个 onListItemClick 方法.我最终为 onListItemClick 方法编写了类似的内容:

I'm hitting an external API that's returning JSON data (new dvd titles). I'm able to parse out the JSON and list each dvd title and other dvd information into a ListView just fine. I was also able to setup an onListItemClick method just fine for primitive data (title string). I ended up writing something like so for the onListItemClick method:

请注意,productArray 是一个类 var,它由另一个包含 JSONObjects 数组的方法设置.

Just to note, the productArray is a class var that's being set by another method that holds an array of JSONObjects.

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();
    }       

}

以上代码都有效,但我认为 GOTTA 是我将数据传递给另一个 Activity 的更好方法.我在想我将能够传递一个包含 DVD 电影的所有数据的 JSONObject,而不是单独设置每个数据点.

The above code all works, but I'm thinking there's GOTTA be a better way for me to pass in data to another Activity. I was thinking that I would be able to pass a JSONObject that contains all the data for a dvd movie instead of setting each data point individually.

我尝试了一个半星期来弄清楚如何使用 Parcelable.我尝试实例化一个新的 JSONObject jsonObj,它实现了 Parcelable,但没有运气.我的 LogCat 中不断出现错误,指出该对象不可打包.

I tried for a week and a half to figure out how to use Parcelable. I tried instantiating a new JSONObject jsonObj that implements Parcelable with no luck. I kept getting an error in my LogCat that said that the object was un-parcelable.

我尝试阅读 Android 开发者网站和其他博客,但我无法将他们的示例应用到我需要做的事情上.

I've tried reading the Android developer site and other blogs, but I couldn't apply their examples to what I needed to do.

任何帮助将不胜感激

推荐答案

你可以将一部电影的所有信息封装到一个Movie对象中,该对象实现了Parcelable.

You can just encapsulate all of the information about a movie into a Movie object, which implements Parcelable.

代码看起来与上面类似,但您可以只传递一个电影,而不是传递 6 个不同的附加内容.

The code will look similar to above, but instead of passing 6 different extras you can just pass one extra that is the movie.

Movie movie = new Movie();
movie.setTitle(jsonObj.getJSONObject("Title").opt("val").toString());
movie.setRelDat(jsonObj.getJSONObject("RelDate").opt("val").toString());
.
.
.
i.putExtra("movie", movie);

有关实现 Parcelable 对象的信息,请参阅 Parcelable 文档.您基本上只需写出 'writeToParcel' 中的每个字符串,然后按正确顺序读入 'readFromParcel' 中的每个字符串.

For information on implementing a Parcelable object, see Parcelable docs. You basically just write out each string in 'writeToParcel', and read in each string in 'readFromParcel' in the correct order.

这篇关于将 JSONObject 传递给另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆