Tutorial of passing Strings or ArrayList or Arrays from one java file to another using Intents in Android..
Here I declared two java files in package and two xml files with respect to java files..
MainActivity.java:
package com.example.ex;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
/*ArrayList for Storing values*/
ArrayList ar=new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button) findViewById(R.id.button1);
/*inserting values into Arraylist*/
for(int i=0;i<10;i++)
{
/*add is method through which values are binded to ArrayList */
ar.add(i);
}
/*setting up OnClickListener and Passing values to Second.java file */
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
/*Declaring an Intent to Start another activity*/
Intent i=new Intent(MainActivity.this,Second.class);
/*putExtra() is method used to add some extra content to Intent variable..
* here as i am passing ArrayList i choose putStringArrayListExtra(),
* this method takes two parameters,first refers to key or id for ArrayList and
* Second is ArrayList name9It is similar for others also */
i.putStringArrayListExtra("key",ar);
/*now i am Starting activity Second */
startActivity(i);
}
});
}
}
Here I declared two java files in package and two xml files with respect to java files..
MainActivity.java:
package com.example.ex;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
/*ArrayList for Storing values*/
ArrayList ar=new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button) findViewById(R.id.button1);
/*inserting values into Arraylist*/
for(int i=0;i<10;i++)
{
/*add is method through which values are binded to ArrayList */
ar.add(i);
}
/*setting up OnClickListener and Passing values to Second.java file */
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
/*Declaring an Intent to Start another activity*/
Intent i=new Intent(MainActivity.this,Second.class);
/*putExtra() is method used to add some extra content to Intent variable..
* here as i am passing ArrayList i choose putStringArrayListExtra(),
* this method takes two parameters,first refers to key or id for ArrayList and
* Second is ArrayList name9It is similar for others also */
i.putStringArrayListExtra("key",ar);
/*now i am Starting activity Second */
startActivity(i);
}
});
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="38dp"
android:layout_marginTop="152dp"
android:text="@string/pas" />
</RelativeLayout>
Second.java:
package com.example.ex;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lay2);
ListView li=(ListView) findViewById(R.id.listView1);
/* Bundle for getting extras sent through Intent from MainActivity*/
Bundle b1=getIntent().getExtras();
/* ArrayList for Storing values which are sent from from MainActivty,
* getStringArrayList() will get values From Intent
* this method takes one parameter which is the first parameter of putStringArrayListExtra()*/
ArrayList s1=b1.getStringArrayList("key");
/*setting values of ArrayList to ListView */
li.setAdapter(new ArrayAdapter(Second.this,android.R.layout.simple_list_item_1,s1));
}
}
lay2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
The output for the above Application is:
CHECK My Android APPS:
NCDupRemover-Backup,Clear all Contacts and Remove Duplicate Contacts..
NLauncherV2.2.2-Multipurpose Launcher
NLauncherCV0.2
NMemoV0.2
Follow me in XDA:
My Profile in XDA-Developers
NCDupRemover-Backup,Clear all Contacts and Remove Duplicate Contacts..
NLauncherV2.2.2-Multipurpose Launcher
NLauncherCV0.2
NMemoV0.2
Follow me in XDA:
My Profile in XDA-Developers