AS中級 値の受け渡し編②解説~Activityから別のActivityへ配列、List(コレクション)、オブジェクトを渡す~
Try1.の解答例
MainActivity.java
package com.example.arraytransition;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int[] intArray = new int[]{3,7,5};
Button nextBtn = findViewById(R.id.nextBtn);
nextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("array",intArray);
startActivity(intent);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="int型の配列 {3,7,5}"
android:layout_marginTop="60sp"/>
<Button
android:id="@+id/nextBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="次ページへ遷移"
android:layout_marginTop="40sp"/>
</LinearLayout>
SecondActivity.java
package com.example.arraytransition;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
int[] intArrayTake = intent.getIntArrayExtra("array");
TextView secondView = findViewById(R.id.secondView);
String viewText = "{" + intArrayTake[0] + "," + intArrayTake[1]+"," + intArrayTake[2] + "}";
secondView.setText(viewText);
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity"
android:gravity="center_horizontal">
<TextView
android:id="@+id/secondView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_marginTop="160sp"/>
</LinearLayout>
Try2の解答例
MainActivity.java
package com.example.arraytransition2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Nameage> nameageList = new ArrayList<>();
nameageList.add(new Nameage("yamada",29));
nameageList.add(new Nameage("satou",34));
nameageList.add(new Nameage("suzuki",67));
TextView firstView = findViewById(R.id.firstView);
StringBuilder firstStrBld = new StringBuilder();
for(Nameage nameage:nameageList){
firstStrBld.append(nameage.getName()).append(" ").append(nameage.getAge()).append("\n");
}
firstView.setText(firstStrBld.toString());
Button nextBtn = findViewById(R.id.nextBtn);
nextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putParcelableArrayListExtra("list",nameageList);
startActivity(intent);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/firstView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/nextBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="次ページへ"/>
</LinearLayout>
SecondActivity.java
package com.example.arraytransition2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import java.util.ArrayList;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
ArrayList<Nameage> arrayList = intent.getParcelableArrayListExtra("list");
TextView secondView = findViewById(R.id.secondView);
StringBuilder strBld = new StringBuilder();
for(Nameage nameage:arrayList){
strBld.append(nameage.getName()).append(" ").append(String.valueOf(nameage.getAge())).append("\n");
}
secondView.setText(strBld.toString());
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity"
android:gravity="center_horizontal">
<TextView
android:id="@+id/secondView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80sp"/>
</LinearLayout>
Nameage.java
package com.example.arraytransition2;
import android.os.Parcel;
import android.os.Parcelable;
public class Nameage implements Parcelable {
private String name;
private int age;
public Nameage(String name, int age) {
this.name = name;
this.age = age;
}
protected Nameage(Parcel in) {
name = in.readString();
age = in.readInt();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<Nameage> CREATOR = new Creator<Nameage>() {
@Override
public Nameage createFromParcel(Parcel in) {
return new Nameage(in);
}
@Override
public Nameage[] newArray(int size) {
return new Nameage[size];
}
};
public String getName() { return name; }
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Try3の解答例