在现有代码中,我有以下类来保存类别详细信息。
import org.json.JSONObject;
import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;
public class Category {
private String mIconURL;
private String mName;
public Category() {
super();
}
public String getIconURL() {
return mIconURL;
}
public void setIconURL(String iconURL) {
this.mIconURL = iconURL;
}
public String getName() {
return mName;
}
public void setName(String name) {
this.mName = name;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeString(mIconURL);
dest.writeString(mName);
}
public static final Parcelable.Creator<Category> CREATOR = new Parcelable.Creator<Category>() {
public Category createFromParcel(Parcel in) {
return new Category(in);
}
public Category[] newArray(int size) {
return new Category[size];
}
};
private Category(Parcel in) {
super(in);
mIconURL = in.readString();
mName = in.readString();
}
@Override
public int describeContents() {
return 1;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("{");
builder.append(super.toString());
builder.append(", mName=").append(mName);
builder.append(", mIconURL=").append(mIconURL);
builder.append("}");
return builder.toString();
}
public static Category parseFromJSON(JSONObject jsonObject) {
Category cat = new Category();
try {
cat.setServerId(jsonObject.getInt("id"));
cat.setName(jsonObject.getString("name"));
cat.setIconURL(jsonObject.getString("icon"));
} catch (Exception e) {
}
return cat;
}
}
并且应用程序工作正常,但现在我想将 Image 属性添加到这个类别类。我是 Java 和 Android 的新手。但是现有的类有一个叫做包裹的东西。现在我如何为位图做同样的事情?是不是像下面这样
public Bitmap getImage()
{
return mImage;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeString(mIconURL);
dest.writeString(mName);
dest.writeByteArray(mImage);// there is no "writeBitmap" method in Parcel.
}
请指导我