IntentのputExtraメソッドを使用して、以前のように、などのコレクションを別のコレクションArrayList
に渡すにはどうすればよいですか?Activity
Strings
int
誰かが私を助けてくれますList<String>
かActivity
?
IntentのputExtraメソッドを使用して、以前のように、などのコレクションを別のコレクションArrayList
に渡すにはどうすればよいですか?Activity
Strings
int
誰かが私を助けてくれますList<String>
かActivity
?
タイプが。ArrayList<E>
の場合、同じ方法で渡すことができます。E
Serializable
putExtra (String name, Serializable value)
を呼び出しIntent
て保存し、getSerializableExtra (String name)
取得します。
例:
ArrayList<String> myList = new ArrayList<String>();
intent.putExtra("mylist", myList);
他のアクティビティ:
ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist");
シリアル化はパフォーマンスの問題を引き起こす可能性があることに注意してください。時間がかかり、多くのオブジェクトが割り当てられます(したがって、ガベージコレクションが必要になります)。
まず、Parcelableオブジェクトクラスを作成する必要があります。例を参照してください
public class Student implements Parcelable {
int id;
String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int arg1) {
// TODO Auto-generated method stub
dest.writeInt(id);
dest.writeString(name);
}
public Student(Parcel in) {
id = in.readInt();
name = in.readString();
}
public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {
public Student createFromParcel(Parcel in) {
return new Student(in);
}
public Student[] newArray(int size) {
return new Student[size];
}
};
}
ArrayList<Student> arraylist = new ArrayList<Student>();
Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("mylist", arraylist);
intent.putExtras(bundle);
this.startActivity(intent);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle bundle = getIntent().getExtras();
ArrayList<Student> arraylist = bundle.getParcelableArrayList("mylist");
}
putExtra
インテントに値を渡すために使用します。メソッドを使用getSerializableExtra
して、このようなデータを取得します
アクティビティA:
ArrayList<String> list = new ArrayList<String>();
intent.putExtra("arraylist", list);
startActivity(intent);
アクティビティB:
ArrayList<String> list = getIntent().getSerializableExtra("arraylist");
提案されたすべての手法を試しましたが、どれも機能せず、アプリが機能しなくなり、ついに成功しました。これが私がそれをした方法です...主な活動で私はこれをしました:
List<String> myList...;
Intent intent = new Intent...;
Bundle b=new Bundle();
b.putStringArrayList("KEY",(ArrayList<String>)myList);
intent_deviceList.putExtras(b);
....startActivity(intent);
新しいアクティビティのデータを取得するには:
List<String> myList...
Bundle b = getIntent().getExtras();
if (b != null) {
myList = bundle.getStringArrayList("KEY");
}
これが誰かを助けることを願っています...
あるアクティビティから別のアクティビティにArrayListを渡すには、次のものを含める必要があります。
intent.putStringArrayListExtra(KEY, list); //where list is ArrayList which you want to pass
アクティビティを開始する前に、別のアクティビティでArrayListを取得するには、次のようにします。
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
temp = bundle.getStringArrayList(KEY); // declare temp as ArrayList
}
これを介してArrayListを渡すことができるようになります。これがお役に立てば幸いです。
ViewModelの使用は、SerializableまたはParcelableよりも優れた修正です