3

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.

8
  • 4
    What is the question here? Commented Oct 30, 2013 at 10:33
  • Are you trying to ask something? Commented Oct 30, 2013 at 10:33
  • I see a lot of code, but I have no idea of what you are trying to achieve and what is your problem. Commented Oct 30, 2013 at 10:34
  • 2
    dunno your culture, but where i grew up a question comes along with a question.. Commented Oct 30, 2013 at 10:34
  • Error is null pointer.When im trying to execute my code. Im facing the null pointer exception. ITs happening in second activity, when im trying to access the following code. 'Intent in = getIntent(); // Get String array name from 1 activity String[] name = in.getStringArrayExtra("name"); // got a problem here t[0].setText(name[i]);' Because im trying to call, String name from main activity. The rest of the xml code is my UI. Commented Oct 30, 2013 at 12:56

5 Answers 5

2

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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

6 Comments

Hey hussain.According to your answer. Im not passing any string values on put string array. As im trying to display the selected item of first activity checkbox in second ?
Ive already passed different, items. If u go to second activity. There is a problem when u trying to call t[i].setText(name[i]);. There im facing null pointer exception. Its workng , if im just trying to pass t[i].setText(name), its just executing last checked checkbox.
@B.rohitNare : see the edit , i'll have to check your very scenario , will answer then
Im not passing array list hussain. Im just passing arrays. Have you been through the code once ?
@B.rohitNare :can I have your layouts ??
|
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.