3

I want to get a value passed from another activity, it works well with multiple type like JSONArray, String, etc... but i could not find the way to retrieve a ArrayList value.

I tried several way like this one with no success:

      ArrayList<JSONObject> poiArr;   
        JSONArray jsonPoints;  
        try {
            poiArr = new ArrayList<JSONObject>(bundle.getSparseParcelableArray("poiArr"));  
//here is the problem 
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            jsonPoints = new JSONArray(bundle.getString("lesPoints"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

I have this error message:

java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference

Any ideas?

ΩlostA
  • 2,501
  • 5
  • 27
  • 63
  • One method I can suggest is to convert your JsonObject to String and pass it using putStringArrayListExtra() – p.mathew13 Oct 07 '19 at 16:32

2 Answers2

2

JSONObject is not Parcelable, or even Serializable. So the only option you're left with is to utilize JSONObject's toString() method and then rely on using putStringArrayListExtra() of Intent to pass it to the other.

On the other activity, use getExtras() of Intent to receive and convert that to JSONObject by passing the String into its constructor & loop to form the ArrayList if required.

Say, the ArrayList of JSONObject is sendingArr :

List<String> sendingListOfStrings = new ArrayList<String>();
for(int i = 0; i < sendingArr.length(); i++) {
    sendingListOfStrings.add( sendingArr.getJSONObject(i).toString() );
}

Sending from ActivityOne to ActivityTwo:

Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
intent.putStringArrayListExtra("sending_list", sendingListOfStrings);
startActivity(intent);

Then capture it = in the other:

ArrayList<JSONObject> receivingArr;
Bundle receivingListOfStrings = getIntent().getExtras();
ArrayList<String> list = receivingListOfStrings.getStringArrayList("sending_list");
for (int i = 0; i < receivingListOfStrings.length(); i++) {
    receivingArr.add( new JSONObject( receivingListOfStrings.get(i) ) );
}
SaadAAkash
  • 3,065
  • 3
  • 19
  • 25
  • Hi, I have two error messages => *get java.lang.string in BaseBundle cannot be applied to (int) for the receivingListOfStrings.get(i)* & => *Cannot resolve method length() for the receivingListOfStrings.length()* – ΩlostA Oct 08 '19 at 16:44
  • I think I can use all of your code except the last loop for that seems not to work. But the rest works well. Thanks – ΩlostA Oct 08 '19 at 16:49
  • 1
    You're welcome @ΩlostA! Thanks for accepting the answer as well. – SaadAAkash Oct 08 '19 at 18:20
0

Instead of passing ArrayList< JSONObject >, you can create a Parcelable/Serializable Java Object(Model) and convert JSONObject to Java Object and then pass ArrayList< Model>. Ultimately you have to convert this JSONObjcet somewhere in your project to use it.

To convert JSONObject to Model, please check this:

How do I convert a JSONObject to class object?

Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46