我有一个自定义对象的arraylist,它扩展了Parecelable类,因此可以通过bundle或localbroadcast manager发送给Activity。
现在,当我尝试发送其他数组的arraylist,ArrayList<ArrayList<my_custom_object>>
时,我的问题就出现了。我怎样才能做到这一点?
答案 0 :(得分:1)
1.Custom parcelable class(替换为您的自定义parcelable类):
import android.os.Parcel;
import android.os.Parcelable;
/**
* Custom parcelable class
*/
public class MyParcelable implements Parcelable {
public int data;
public MyParcelable(int data) {
this.data = data;
}
private MyParcelable(Parcel source) {
// Reading custom data
data = source.readInt();
}
public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
@Override
public MyParcelable createFromParcel(Parcel source) {
return new MyParcelable(source);
}
@Override
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// Saving custom data
dest.writeInt(data);
}
}
2.实现扩展ArrayList的Parcelable类。
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
/**
* ArrayList of the custom parcelable object
*/
public class MyParcelableList extends ArrayList<MyParcelable> implements Parcelable {
public MyParcelableList() {
}
private MyParcelableList(Parcel source) {
for (Parcelable p : source.readParcelableArray(MyParcelable.class.getClassLoader()))
add((MyParcelable) p);
}
public static final Parcelable.Creator<MyParcelableList> CREATOR
= new Parcelable.Creator<MyParcelableList>() {
@Override
public MyParcelableList createFromParcel(Parcel source) {
return new MyParcelableList(source);
}
@Override
public MyParcelableList[] newArray(int size) {
return new MyParcelableList[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelableArray(toArray(new MyParcelable[size()]), flags);
}
}
3.Passing Intent to TargetActivity。
// Preparing data ...
MyParcelableList list1 = new MyParcelableList();
list1.add(new MyParcelable(1));
MyParcelableList list2 = new MyParcelableList();
list2.add(new MyParcelable(2));
list2.add(new MyParcelable(3));
ArrayList<MyParcelableList> listOfList = new ArrayList<>();
listOfList.add(list1);
listOfList.add(list2);
// Send intent to your target activity
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("KEY_OF_EXTRA", listOfList);
context.startActivity(new Intent(context, TargetActivity.class).putExtras(bundle));
4.在TargetActivity上检索数据。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// processing intent data
if (getIntent()!=null) {
Bundle bundle = getIntent().getExtras();
if (bundle.containsKey("KEY_OF_EXTRA")) {
ArrayList<MyParcelableList> listOfList = bundle.getParcelableArrayList("KEY_OF_EXTRA");
if (listOfList != null)
// Do something here ...
for (MyParcelableList list : listOfList) {
for (MyParcelable p : list) {
Log.v("TargetActivity", "p.data=" + p.data);
}
}
}
}
}
答案 1 :(得分:0)
您可以尝试以下代码
ActivityA发送:
5.024
ActivityB get:
ArrayList<ArrayList<CustomClass>>data = new ArrayList<ArrayList<CustomClass>>();
ArrayList<CustomClass>d1 = new ArrayList<CustomClass>();
CustomClass temp = new CustomClass("this is my object");
d1.add(temp);
data.add(d1);
Intent intent = new Intent(JumpToMainActivity.ACTION);
intent.putExtra("data",data);
startActivity(intent);
CusstonClass.java:
Intent intent = getIntent();
ArrayList<ArrayList<CustomClass>>data = (ArrayList<ArrayList<CustomClass>>) intent.getSerializableExtra("data");
ArrayList<CustomClass>d1 = data.get(0);
System.out.println("received:" + d1.get(0).result);
希望能帮到你