B.rohit Nare
B.rohit Nare

Reputation: 111

Android programming. Passing a array from one activity to other

How do I pass an array between these two activities

Main ACtivity

package com.Rohit.intentpurchase;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

CheckBox c[] = new CheckBox[4];
int[] price = new int [] {20,50,60,80};
int sum = 0;
int num = 0;
String name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b= (Button)findViewById(R.id.button1);
    // Created reference for checkbox
    c[0] = (CheckBox)findViewById(R.id.checkBox1);
    c[1] = (CheckBox)findViewById(R.id.checkBox2);
    c[2] = (CheckBox)findViewById(R.id.checkBox3);
    c[3] = (CheckBox)findViewById(R.id.checkBox4);
    // even handling for button
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
    for(int i = 0; i < c.length; i++) { 
                // on user checked particular box
                if(c[i].isChecked()) {
                    // Get certain box checked
                    name = c[i].getText().toString();
                    // Total of price
                    sum = sum + price[i];
                    // No.of items selected
                    num = num + 1;
                }
            // sending the data to second activity
            Intent in = new Intent(getApplicationContext(), SecondActiviy.class);
            // converted to in.putExtra(String name , string[] value)
            in.putExtra("name", name);
            in.putExtra("Price",sum);
            in.putExtra("Total",num);
            startActivity(in);

    }

        }
    });
}

}``

Second Activity
package com.Rohit.intentpurchase;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.TextView;

public class SecondActiviy extends Activity {
// created an array for Textbox
TextView t[] = new TextView[6];
int i,num,sum;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second_activiy);
    t[0] = (TextView)findViewById(R.id.textView2);
    t[1] = (TextView)findViewById(R.id.textView3);
    t[2] = (TextView)findViewById(R.id.textView4);
    t[3] = (TextView)findViewById(R.id.textView5);
    t[4] = (TextView)findViewById(R.id.textView6);
    t[5] = (TextView)findViewById(R.id.textView7);

     for(int j = 0; j < t.length; j++) {    
        // Get access to data from 1 activity
         Intent in = getIntent();
        // Get String array name from 1 activity
         String[] name = in.getStringArrayExtra("name");
        // got a problem here
        t[0].setText(name[i]);
    // no problem here 
        int total = in.getIntExtra("Total",num);
        int price = in.getIntExtra("Price",sum);

        t[4].setText(" TOTAL: "+ total);
        t[5].setText(" PRICE: "+ price); 
     }



}
}

Xml 
Files Main activity

<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" >

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:text="@string/CB1" />

<CheckBox
    android:id="@+id/checkBox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/checkBox1"
    android:layout_below="@+id/checkBox1"
    android:text="@string/CB2" />

<CheckBox
    android:id="@+id/checkBox3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/checkBox2"
    android:layout_below="@+id/checkBox2"
    android:text="@string/CB3" />

<CheckBox
    android:id="@+id/checkBox4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/checkBox3"
    android:layout_below="@+id/checkBox3"
    android:text="@string/CB4" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/checkBox4"
    android:layout_toRightOf="@+id/checkBox4"
    android:text="@string/Button" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="23dp"
    android:text="@string/Items" />

Second Activity 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=".SecondActiviy" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="@string/si" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    android:text="@string/T2" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="16dp"
    android:text="@string/T3" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView3"
    android:layout_below="@+id/textView3"
    android:layout_marginTop="21dp"
    android:text="@string/T4" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView4"
    android:layout_below="@+id/textView4"
    android:layout_marginTop="22dp"
    android:text="@string/T5" />

<TextView
    android:id="@+id/textView7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView6"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/textView1"
    android:text="@string/price" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView5"
    android:layout_below="@+id/textView5"
    android:layout_marginTop="15dp"
    android:text="@string/Items" />

    </RelativeLayout> '`
    --->

These above are xml files by name mainactivity and second activity. Just to show enduser, how my application looks.

Upvotes: 3

Views: 2335

Answers (5)

Piko
Piko

Reputation: 31

In the second activity you are not changing the value of index i, so it is always initialized to 0.

Upvotes: 0

EProgrammerNotFound
EProgrammerNotFound

Reputation: 2461

The problem is the following:

In the first activity you created an array of 4 indexes:

public class MainActivity extends Activity {

CheckBox c[] = new CheckBox[4]; //HERE!!

low bound c = 0
high bound c = 3;

But, in the second activity you are trying to access 4 and 5 indexes because in the second activity the high bound of the array is 5

public class SecondActiviy extends Activity {
// created an array for Textbox
TextView t[] = new TextView[6];

low bound t = 0
high bound t = 5;

  for(int j = 0; j < t.length; j++) {    
        // Get access to data from 1 activity
         Intent in = getIntent();
        // Get String array name from 1 activity
         String[] name = in.getStringArrayExtra("name");
        // got a problem here
        t[0].setText(name[i]);
        // no problem here 
        int total = in.getIntExtra("Total",num);
        int price = in.getIntExtra("Price",sum);

        t[4].setText(" TOTAL: "+ total);
        t[5].setText(" PRICE: "+ price); 
     }

So, you cannot do This:

// got a problem here
t[0].setText(name[i]);

Because when j = 4 or j = 5, it will trespass the highest index of the array "name".

Upvotes: 0

you want this ??

to pass from activity 1 :

Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);

To Read From Activity 2:

Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);

in activity 1 : List tempArrayList = new Arraylist();

tempArrayList.add(name);
tempArrayList.add(price);
tempArrayList.add(total);

now

Bundle b=new Bundle();
    b.putStringArray(key, tempArrayList);
    Intent i=new Intent(context, Class);
    i.putExtras(b);

in activity 2 :

Bundle b=this.getIntent().getExtras();
    String[] array=b.getStringArray("tempArrayList");

Upvotes: 0

Linga
Linga

Reputation: 10553

For passing, in your MAinactivity try something like

Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent((getApplicationContext(), SecondActiviy.class);
i.putExtras(b);

And for receiveing in your SecondActivity

Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);

Upvotes: 2

schlingel
schlingel

Reputation: 8575

Send a Bundle containing the data. Have a look at the documentation. putIntegerArrayList looks promising.

Upvotes: 0

Related Questions