将 JSONObject 传递到另一个活动中
我正在使用一个返回 JSON 数据(新的 DVD 标题)的外部 API。我能够解析 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();
}
}
上面的代码都有效,但我认为有更好的方法让我将数据传递给另一个活动。我认为我能够传递一个包含 DVD 电影的所有数据的 JSONObject,而不是单独设置每个数据点。
我尝试了一个半星期才弄清楚如何使用 Parcelable。我尝试实例化一个新的 JSONObject jsonObj 来实现 Parcelable,但没有成功。我的 LogCat 中不断收到错误,指出该对象无法打包。
我尝试阅读 Android 开发人员网站和其他博客,但我无法将他们的示例应用于我需要做的事情。
任何帮助将不胜感激
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:
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();
}
}
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.
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.
I've tried reading the Android developer site and other blogs, but I couldn't apply their examples to what I needed to do.
Any help would be much appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以将有关电影的所有信息封装到一个实现
Parcelable
的Movie
对象中。该代码看起来与上面类似,但您可以只传递一个额外的电影,而不是传递 6 个不同的额外内容。
有关实现 Parcelable 对象的信息,请参阅 Parcelable 文档。您基本上只需写出“writeToParcel”中的每个字符串,然后以正确的顺序读入“readFromParcel”中的每个字符串。
You can just encapsulate all of the information about a movie into a
Movie
object, which implementsParcelable
.The code will look similar to above, but instead of passing 6 different extras you can just pass one extra that is the movie.
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.
Cheryl Simon 在这里发布的答案是完全正确的,但是对于 Android 或 Java 新手来说,这可能无法清楚地解释它。请让我以她的回答为基础。
最好、最简洁的方法是使用 Parcelable 接口。
声明一个实现 Parcelable 的类,如下所示。这个使用了实际上不必要的访问器和修改器。
下一步是创建该类的实例(在本例中为 JsonArrayParser)并将其通过 Bundle 传递,如下例所示。
//这可以是 Activity、Fragment、DialogFragment 或使用 Bundle Type 的任何类,例如savedInstanceState。
您检索值的方式如下(我使用了 onCreate,但您可以在任何地方检索值):
作为附加说明:您可以使用自定义构造函数来执行此操作,但 Google 已弃用此方法。
旧的已弃用方法(与应用程序兼容 v4 不推荐相关)是:
希望这会有所帮助并解决一些问题。
Parcelable 接口可以与带有或不带有修改器和/或访问器的任何数据类一起使用。
The answer posted here by Cheryl Simon is completely correct however this might not explain it too clearly for someone who is new to android or java. Please let me build on her answer.
The best and cleanest way to do this is with the Parcelable interface.
Declare a class as below that implements Parcelable. This one uses an accessor and mutator which isn't actually necessary.
The next step would be to create an instance of that class (in this case JSonArrayParser) and pass it through the Bundle as in the example below.
//This could be an Activity, Fragment, DialogFragment or any class that uses the Bundle Type such as savedInstanceState.
The the way your would retrieve the value would be as follows (I've used the onCreate but you can retrieve the value anywhere):
Just as an additional note: you can do this with custom constructors, but this method has been deprecated by Google.
The old deprecated method (associated with app compat v4 NOT RECOMMENDED) would be:
Hope this help and clears a few things up.
The Parcelable interface can be used with any data class with or without mutators and/or accessors.
您是否将信息存储在数据库中?如果是的话,您可以简单地传递所需标题的 ID(通过意图)。
Are you storing the information in a DB? If you are, you can simply pass the ID of the desired title (via the intent).
看看gson。它允许您将 JSON blob 序列化和反序列化为整个类实例。
Have a look at gson. It allows you to sereialise and deserialise JSON blobs to entire class instances.
只需像这样创建一个 Parcel:
并放入捆绑包中:
最后,您可以使用以下命令取回 JSONObject:
Just create a Parcel like this:
And put into the bundle:
Finally, you can get the JSONObject back using:
您可以简单地将整个 JSONObject 作为字符串。像这样的东西:
i.putString("产品", jsonObj.toString);
然后在
MovieProductActivity
中你可以JSONObject jsonObj = new JSONObject(getIntent().getStringExtra("产品"));
You can simply put an entire JSONObject as a string. Something like this:
i.putString("product", jsonObj.toString);
And then in the
MovieProductActivity
you couldJSONObject jsonObj = new JSONObject(getIntent().getStringExtra("product"));