我正在尝试将对象列表从一个 Activity 传递到另一个 Activity ,这是我尝试过的但出现错误:
Error:(61, 23) 错误:类 Bundle 中的方法 putParcelable 不能应用于给定的类型; required: String,Parcelable found: String,List 原因:实参List无法通过方法调用转换为Parcelable
private ListView listView1;
List <ListObject> listObjects = new ArrayList<ListObject>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Intent i = new Intent("com.tutorial.details");
// startActivity(i);
Intent intent = new Intent(getApplicationContext(),DetailsActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("data", listObjects);
intent.putExtras(bundle);
startActivity(intent);
}
});
...... }
class ListObject implements Parcelable{
public String title;
public String items;
public String remaining;
public ListObject(){
super();
}
public ListObject(String title, String items, String remaining) {
super();
try {
// this.icon = icon;
this.title = title;
this.items = items;
this.remaining = remaining;
if(Integer.parseInt(remaining)> Integer.parseInt(items)) {
this.remaining = items;
throw new IllegalArgumentException();
}
}catch(IllegalArgumentException e){
System.out.println("items = "+ items + " remaining: "+remaining);
e.printStackTrace();
}
}
public ListObject(Parcel in)
{
}
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(title);
parcel.writeString(items);
parcel.writeString(remaining);
}
public int describeContents() {
return 0;
}
public final Parcelable.Creator<ListObject> CREATOR = new Parcelable.Creator<ListObject>()
{
public ListObject createFromParcel(Parcel in)
{
return new ListObject(in);
}
public ListObject[] newArray(int size)
{
return new ListObject[size];
}
};
}
最佳答案
或者创建一个 Parcelable
延伸 ArrayList<ListObject>
并使用
ListObjectList listObjectsList = new ListObjectList();
...
bundle.putParcelable("data", listObjectList);
ListObject类:
class ListObject implements Parcelable{
public String title;
public String items;
public String remaining;
public ListObject(){
super();
}
public ListObject(String title, String items, String remaining) {
super();
this.title = title;
this.items = items;
this.remaining = remaining;
}
// add getter and setter
public ListObject(Parcel in) {
this();
readFromParcel(in);
}
private void readFromParcel(Parcel in) {
this.title = in.readString();
this.items = in.readString();
this.remaining = in.readString();
}
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(title);
parcel.writeString(items);
parcel.writeString(remaining);
}
public int describeContents() {
return 0;
}
public final Parcelable.Creator<ListObject> CREATOR = new Parcelable.Creator<ListObject>()
{
public ListObject createFromParcel(Parcel in)
{
return new ListObject(in);
}
public ListObject[] newArray(int size)
{
return new ListObject[size];
}
};
}
ListObjectList 类:
public class ListObjectList extends ArrayList<ListObject> implements Parcelable {
public ListObjectList() {
}
public ListObjectList(Parcel in) {
this();
readFromParcel(in);
}
private void readFromParcel(Parcel in) {
this.clear();
int size = in.readInt();
for (int n = 0; n < size; n++) {
ListObject item = new ListObject(in.readString(), in.readString(), in.readString());
this.add(item);
}
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel parcel, int flags) {
int size = this.size();
parcel.writeInt(size);
for (int n = 0; n < size; n++) {
ListObject item = this.get(n);
parcel.writeString(item.getTitle());
parcel.writeDouble(item.getItems());
parcel.writeDouble(item.getRemaining());
}
}
public final Parcelable.Creator<ListObjectList> CREATOR = new Parcelable.Creator<ListObjectList>() {
public ListObjectList createFromParcel(Parcel in) {
return new ListObjectList(in);
}
public ListObjectList[] newArray(int size) {
return new ListObjectList[size];
}
};
}
关于android - 如何在 Android 中的两个 Activity 之间传递对象列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29085700/