Passing JSONObject into another activity

后端 未结 7 1822
情歌与酒
情歌与酒 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:06

    Just create a Parcel like this:

    public class Parcel implements Serializable {
    private JSONObject obj;
    
    public Parcel(JSONObject obj) {
        this.obj = obj;
    }
    
    public JSONObject getObj() {
        return obj;
    }}
    

    And put into the bundle:

    Parcel p = new Parcel(data);
        Bundle args = new Bundle();
        args.putSerializable("events", p);
    

    Finally, you can get the JSONObject back using:

    JSONObject obj = ((Parcel) ((Bundle) getArguments()).getSerializable("events")).getObj();
    
    0 讨论(0)
  • 2020-11-30 05:08

    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.

    public class JSonArrayParser implements Parcelable {
    
        private JSONArray jsonArray;
        public JSonArrayParser() {}
    
        public JSonArrayParser(JSONArray jsonArray) {
            this.jsonArray = jsonArray;
        }
    
        public void setJsonArray(JSONArray jsonArray) {
            this.jsonArray = jsonArray;
        }
    
        public JSONArray getJsonArray() {
            return jsonArray;
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
    
        }
    }
    

    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.

    jobItemsPopUp = new JobItemsPopUp();//This example uses a DialogFragment.
    Bundle bundle = new Bundle();
    JSonArrayParser jSonArrayParser = new JSonArrayParser(jsonArray);
    bundle.putParcelable("jsonArray", jSonArrayParser);
    jobItemsPopUp.setArguments(bundle);
    jobItemsPopUp.show(getActivity().getSupportFragmentManager(), "JobItemsPopUp");
    

    The the way your would retrieve the value would be as follows (I've used the onCreate but you can retrieve the value anywhere):

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            this.jsonArray = ((JSonArrayParser) getArguments().getParcelable("jsonArray")).getJsonArray();
        }
    }
    

    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:

    private Fragment MyFragment;
    MyFragment = new MyFragment(jsonArray)
    

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 05:12

    From Current Activity

    Intent mIntent = new Intent(CurrentActivity.this, TargetActivity.class);
    mIntent.putExtra("ITEM_EXTRA", json_object.toString());
    startActivity(mIntent);
    

    From Target Activity's onCreate

    try {
       json_object = new JSONObject(getIntent().getStringExtra("ITEM_EXTRA"));
    
       Log.e(TAG, "Example Item: " + json_object.getString("KEY"));
    
    } catch (JSONException e) {
       e.printStackTrace();
    }
    
    0 讨论(0)
  • 2020-11-30 05:15

    Are you storing the information in a DB? If you are, you can simply pass the ID of the desired title (via the intent).

    0 讨论(0)
  • 2020-11-30 05:25

    Have a look at gson. It allows you to sereialise and deserialise JSON blobs to entire class instances.

    0 讨论(0)
提交回复
热议问题