これら 2 つのアクティビティ間で配列を渡すにはどうすればよいですか
主な活動
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);
}
}
});
}
}``
2 番目のアクティビティ
パッケージ 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 と second activity という名前の xml ファイルです。エンドユーザーに、私のアプリケーションがどのように見えるかを示すためだけに。