Unable to Pass an ArrayList of JSONObjects to new Activity

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.

Answers


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)


Need Your Help

Issue with the EnabledDays in the Calendar widget

json outsystems

I am using the Enabled days in the Calenadr widget , the enabled days are the days when the data load happens. Data Load dates are stored in the entity and whenever the dataload happended those dates

FTP upload file works manually, but fails using Python ftplib

python ftp

I installed vsFTP in a Debian box. When manually upload file using ftp command, it's ok. i.e, the following session works: