Android编程。将数组从一个活动传递到另一个活动

时间:2013-10-30 10:32:07

标签: java android eclipse android-layout android-intent

如何在这两个活动之间传递数组

主要活动

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

    }

        }
    });
}

}``

第二项活动
    包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> '`
    --->

以上是按名称mainactivity和第二个活动的xml文件。只是为了向最终用户展示我的应用程序的外观。

5 个答案:

答案 0 :(得分:2)

对于传球,在你的MAinactivity尝试类似

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

并在第二次活动中接收

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

答案 1 :(得分:0)

发送包含数据的Bundle。看看文档。 putIntegerArrayList看起来很有希望。

答案 2 :(得分:0)

你想要这个??

从活动1传递:

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

从活动2阅读:

Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);
活动1中的

: 列出tempArrayList = new Arraylist();

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

现在

Bundle b=new Bundle();
    b.putStringArray(key, tempArrayList);
    Intent i=new Intent(context, Class);
    i.putExtras(b);
活动2中的

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

答案 3 :(得分:0)

问题如下:

在第一个活动中,您创建了一个包含4个索引的数组:

public class MainActivity extends Activity {

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

低限c = 0
  高界c = 3;

但是,在第二个活动中,您尝试访问4个和5个索引,因为在第二个活动中,数组的上限是5

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

下限t = 0
  上限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); 
     }

所以,你不能这样做:

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

因为当j = 4j = 5时,它会侵入数组“name”的最高索引

答案 4 :(得分:0)

在第二个活动中,您没有更改索引i的值,因此它始终初始化为0.