Category Archives: Arrays

How to pass an arraylist value from one activity to another in android?

This simple example shows how to pass an ArrayList from one activity to another in Android.
This is the first activity on which we are sending the arrayList.

package com.coderzheaven.passarraylist;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final ArrayList<String> arr = new ArrayList<String>();
        arr.add("Hello");
        arr.add("CoderzHeaven");
        
        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, Second.class);
				intent.putExtra("array_list", arr);
				startActivity(intent);
				
			}
		});
        
        
    }
}

Now this is the second activity in which we are receiving the passed arrayList.

package com.coderzheaven.passarraylist;

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;

public class Second extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
        
        Bundle b = getIntent().getExtras();
        
        if(b!=null){
        	ArrayList<String> arr = (ArrayList<String>)b.getStringArrayList("array_list");
        	System.out.println(arr);
        }    
     
    }
}

This is the AndroidManifest file.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.coderzheaven.passarraylist"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity android:name=".Second"></activity>
    </application>

</manifest>

Please check the LogCat for the Output.

Download complete tutorial PDF from below.

Pass ArrayList Tutorial PDF

All about ArrayList – Android.

package com.coderzheaven.arraylistcomplete;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		ArrayList<String> al = new ArrayList<String>();

		System.out.println("Initial size of al: " + al.size());

		al.add("C");
		al.add("A");
		al.add("E");
		al.add("B");
		al.add("D");
		al.add("F");
		al.add(1, "A2");

		System.out.println("Size of al after additions: " + al.size());

		System.out.println("Contents of al: " + al);

		al.remove("F");
		al.remove(2);

		System.out.println("Size of al after deletions: " + al.size());
		System.out.println("Contents of al: " + al);

		// *********************************************************//

		List<String> list = new ArrayList<String>();
		list.add("A");
		list.add("B");
		list.add("C");
		List<String> list2 = new ArrayList<String>();
		list2.add("X");
		list2.add("Y");
		list2.add("Z");
		list.addAll(list2);
		list.addAll(1, list2);

		System.out.println(list);

		// **********************************************************//

		ArrayList<String> arrayList = new ArrayList<String>();
		arrayList.add("1");
		arrayList.add("2");
		arrayList.add("3");

		Vector<String> v = new Vector<String>();
		v.add("4");
		v.add("5");

		// insert all elements of Vector to ArrayList at index 1
		arrayList.addAll(1, v);

		for (String str : arrayList)
			System.out.println(str);

		list.clear();

		System.out.println("After Clearing");

		for (String str : arrayList)
			System.out.println(str);

		// ****************************************************************//

		ArrayList myList = new ArrayList(5);
		for (int j = 0; j < 5; j++) {
			myList.add(new Integer(j));
		}
		System.out.println("List contains " + myList.size() + " elements");

		Integer int2 = new Integer(2);
		System.out
				.println("List contains Integer(2): " + myList.contains(int2));
		System.out.println("Integer(2) is at index " + myList.indexOf(int2));

		myList.set(2, new Integer(99));
		System.out.println("Get element at index 2: " + myList.get(2));

		myList.ensureCapacity(15);

		for (int k = myList.size(); k < 25; k++) {
			myList.add(k, new Integer(k));
		}

		System.out.println(myList);

		myList.subList(10, 14).clear();
		myList.trimToSize();

		// ****************************************************************//

		IteratorDemo();

		checkEmpty();
		
		ListIteratorDemo();
		
		

	}

	void IteratorDemo() {
		ArrayList<String> al = new ArrayList<String>();

		al.add("C");
		al.add("A");
		al.add("E");
		al.add("B");
		al.add("D");
		al.add("F");

		System.out.print("Original contents of al: ");
		Iterator<String> itr = al.iterator();
		while (itr.hasNext()) {
			String element = itr.next();
			System.out.print(element + " ");
		}
		System.out.println();

		ListIterator<String> litr = al.listIterator();
		while (litr.hasNext()) {
			String element = litr.next();
			litr.set(element + "+");
		}

		// Now, display the list backwards.
		System.out.print("Modified list backwards: ");
		while (litr.hasPrevious()) {
			String element = litr.previous();
			System.out.print(element + " ");
		}
	}

	void checkEmpty() {
		List list = Arrays.asList(new String[] { "A", "B", "C", "D" });
		System.out.println(list.size());
		System.out.println(list.isEmpty());
	}

	void ListIteratorDemo() {
		ArrayList<String> al = new ArrayList<String>();

		al.add("C");
		al.add("A");
		al.add("E");
		al.add("B");
		al.add("D");
		al.add("F");

		System.out.print("Original contents of al: ");
		Iterator<String> itr = al.iterator();
		while (itr.hasNext()) {
			String element = itr.next();
			System.out.print(element + " ");
		}
		System.out.println();

		ListIterator<String> litr = al.listIterator();
		while (litr.hasNext()) {
			String element = litr.next();
			litr.set(element + "+");
		}

		// Now, display the list backwards.
		System.out.print("Modified list backwards: ");
		while (litr.hasPrevious()) {
			String element = litr.previous();
			System.out.print(element + " ");
		}
	}


}

Download complete tutorial PDF from here.

Please check the LogCat for Output.