路过的JSONObject到另一个活动JSONObject

2023-09-12 04:28:39 作者:寄情书

我打一个外部的API,返回JSON数据(新的DVD标题)。我能够解析出JSON,并列出到一个ListView就好每张DVD标题和其他DVD的信息。我还能够设置一个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是正在被由持有一个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();
    }       

}

以上code中所有的作品,但我想有一定有对我来说,在数据传递到另一个活动更好的办法。我在想,我将能够通过一个包含所有数据的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中一个错误,说对象是联合国parcelable。

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.

任何帮助将是非常美联社preciated

Any help would be much appreciated

推荐答案

您可以只封装了所有的有关电影到电影的对象,它实现 Parcelable

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

在code将类似于上面,但不是通过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.