Passing JSONObject into another activity

后端 未结 7 1957
情歌与酒
情歌与酒 2020-11-30 04:56

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

7条回答
  •  囚心锁ツ
    2020-11-30 05:11

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

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

    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.

提交回复
热议问题