我创建了一个实现了Parcelable的“ Item”类,并设置了他的构造器
public Item(Parcel in) {
this.mName = in.readString();
this.mFilePath = in.readString();
this.mId = in.readInt();
this.mLength = in.readInt();
this.mTime = in.readLong();
}
我的问题是,当我使用该类时,我不知道如何传递参数,例如波纹管:
Item item = new Item(//What to put her!);
item.setId(c.getInt(c.getColumnIndex("_id")));
item.setName(c.getString(c.getColumnIndex(DBHelperItem.COLUMN_NAME_RECORDING_NAME)));
item.setFilePath(c.getString(c.getColumnIndex(DBHelperItem.COLUMN_NAME_RECORDING_FILE_PATH)));
item.setLength(c.getInt(c.getColumnIndex(DBHelperItem.COLUMN_NAME_RECORDING_LENGTH)));
item.setTime(c.getLong(c.getColumnIndex(DBHelperItem.COLUMN_NAME_TIME_ADDED)));
return item;
答案 0 :(得分:0)
使用Parcel.writeStringArray捆绑您的字符串,然后将其粘贴到构造函数中
答案 1 :(得分:0)
您可以在Item类中具有其他构造函数
public Item(mName,mFilepath,mId,mLength,mTime) {
this.mName = mname;
this.mFilePath = mFilepath;
this.mId = mId;
this.mLength = mLength;
this.mTime = mTime;
}
在您的活动中初始化项目中的所有字段
int id=(c.getInt(c.getColumnIndex("_id")));
String name =
(c.getString(c.getColumnIndex(DBHelperItem.COLUMN_NAME_RECORDING_NAME)));
String filePath=
(c.getString(c.getColumnIndex(DBHelperItem.COLUMN_NAME_RECORDING_FILE_PATH)));
int length=
(c.getInt(c.getColumnIndex(DBHelperItem.COLUMN_NAME_RECORDING_LENGTH)));
double time =
(c.getLong(c.getColumnIndex(DBHelperItem.COLUMN_NAME_TIME_ADDED)));
现在您可以使用以上字段创建新项目
Item item = new Item(id,name,filePath,lenfth,time);
以下是我使用Parcelable的示例:https://github.com/riyaza15/ParcelableDemo enter code here
答案 2 :(得分:0)
Item.java
public class Item {
private String mName;
private String mFilePath;
private int mId;
private int mLength;
private long mTime;
public Item(Parcel in) {
this.mName = in.readString();
this.mFilePath = in.readString();
this.mId = in.readInt();
this.mLength = in.readInt();
this.mTime = in.readLong();
}
}
Parcel.java(根据我们从您的代码中看到的内容,它应该包含以下方法)
public class Parcel {
public String readString() {
//read a string
return null;
}
public int readInt() {
//read an int
return 0;
}
public long readLong() {
//read a long
return 0;
}
}
Test.java(您的主要方法所驻留的)
public class Test {
public static void main(String[] args) {
Parcel p = new Parcel();
/*Pass the parcel like this*/
Item item = new Item(p);
/*
*
* Rest of the code
*
* */
}
}
也许您的方法定义与提供的代码不同。您应该相应地更改它们。