4

I am having trouble passing an ArrayList<JSONObject> to a new activity.

In my Search activity I use

intent.putParcelableArrayListExtra("data", resultsArray);

But I get a Wrong Argument error.

I used this SO question as a reference. Intent.putExtra List

public class SearchActivity extends AppCompatActivity {

    List<JSONObject> resultsArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //other stuff
        resultsArray = new ArrayList<JSONObject>();

    }


    public void goToActivity(){
            Intent intent = new Intent(SearchActivity.this, SearchResultsListActivity.class);
            intent.putParcelableArrayListExtra("data", resultsArray);
            startActivity(intent);
    }

    // ...

}

My SearchResultsListAcivity is just a list

public class SearchResultsListAcivity extends AppCompatActivity {

    public ArrayList<JSONObject> searchResultsArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // .. .

        searchResultsArray = getStringArrayListExtra("data");

        ArrayAdapter<JSONObject> adapter = new ArrayAdapter<JSONObject>(this, android.R.layout.simple_list_item_1, android.R.id.text1, searchResultsArray);
        ListView lv = (ListView) findViewById(R.id.listViewSearchResults);
        lv.setAdapter(adapter);

    }
}

I also have toyed with the implmentation answer listed here How to pass ArrayList of Custom objects to new activity?:

Intent intent = new Intent(getApplicationContext(), displayImage.class);            
Bundle bundleObject = new Bundle();
bundleObject.putSerializable("KEY", arrayList);
intent.putExtras(bundleObject);

But again I get wrong argument error.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user-44651
  • 3,924
  • 6
  • 41
  • 87
  • `JSONObject` does not implement `Parcelable` or `Serializable`. You'll have to pass it as a `String[]` or something and manage serialization and deserialization yourself. – Kevin Krumwiede Mar 15 '16 at 19:06

1 Answers1

2

I would just transform your JSON array into a String array and then pass that to your next activity using putStringArrayListExtra(). You can decode it back to JSON array in your SearchResultsListActivity activity.

i) Transform your JSON array (or list) into a String array

JSONArray arr = new JSONArray(yourJSONresponse);
List<String> list = new ArrayList<String>();
for(int i = 0; i < arr.length(); i++){
list.add(arr.getJSONObject(i).toString());}

ii) Pass it to your SearchResultsListActivity activity as extra

Intent intent = new Intent(SearchActivity.this, SearchResultsListActivity.class);
intent.putStringArrayListExtra("string_array", list);
startActivity(intent);

iii) Finally decode it in your next activity

Bundle stringArrayList = getIntent().getExtras();
ArrayList<String> list = stringArrayList.getStringArrayList("string_array");
JSONArray arr = new JSONArray(list);

I have not tried this code but at least it should give you an idea on how to solve your problem. You might have to make minor changes (the last line maybe replace it for a for loop if it doesn't work, etc)

Benjamin Fell
  • 393
  • 2
  • 11