user4729909
user4729909

Reputation:

pass Arraylist through intent

I want to pass the taskItems

ArrayList<HashMap<String,String>> taskItems = new ArrayList<HashMap<String, String>>();

through intent to the second activity

    ArrayList<String> stringArr = new ArrayList<String>();
    stringArr.add(KEY_TITLE);
    stringArr.add(KEY_INFO);
    stringArr.add(KEY_OBJECT);
    stringArr.add(KEY_LOCATION);

    ArrayList<Integer> intArr = new ArrayList<Integer>();
    intArr.add(R.id.title);
    intArr.add(R.id.info);
    intArr.add(R.id.object);
    intArr.add(R.id.location);

    //Neue Oberfläche starten
    Intent in = new Intent(ListViewActivity.this, ListMenuItemActivity.class);
    in.putStringArrayListExtra("StringAdapter",stringArr);
    in.putIntegerArrayListExtra("intAdapter",intArr);
    ------>in.putStringArrayListExtra("taskItems",taskItems);

    startActivity(in);

I've marked the spot. How can i pass the ArrayList through Intent to my Activity?

Upvotes: 0

Views: 2427

Answers (2)

Jignesh Jain
Jignesh Jain

Reputation: 1568

do this way

Send

ArrayList<HashMap<String,String>> hashMap= new ArrayList<HashMap<String, String>>()

Intent intent = new Intent(SourceActivity.this, DestinationActivity.class);
intent.putExtra("hashMap", hashMap);
startActivity(intent);

Recieve

  Intent intent = getIntent();    
   ArrayList<HashMap<String,String>> hashMap = (ArrayList(HashMap<String, String>)) intent.getSerializableExtra("hashMap");

Upvotes: 1

Simas
Simas

Reputation: 44118

HashMap extends Serializable interface, so you can pass an ArrayList of Serializable objects:

ArrayList<HashMap<String, String>> taskItems = new ArrayList<>();
in.putExtra("taskItems", taskItems);

and fetch it like:

ArrayList<HashMap<String, String>> taskItems = (ArrayList<HashMap<String, String>>) in
        .getSerializableExtra("taskItems");

Upvotes: 2

Related Questions