我有一个自定义对象卡()` 公共卡(字符串元素,字符串颜色,int ResourceId,int CardNumber,字符串特殊){
mResourceId = ResourceId ;
mCardNumber = CardNumber ;
mElement = Element ;
mColor = Color ;
mSpecial=Special;
}`
mResourceId是Card图像的可绘制对象 当我将Card类型的Arraylist保存为Shared Preference,然后将单个图像添加到drawable文件夹并更新应用程序时,Arraylist的所有Card ResourceId混合在一起并且无法正确显示The normal images of the arraylist of Cards
我确定我添加到drawable文件夹中的新图像名称与现有图像的任何名称都不匹配
public class Card implements Parcelable{
private int mResourceId, mCardNumber;
private String mColor, mElement,mSpecial,mResId;
public Card(String Element , String Color , int ResourceId , int CardNumber ){
mResourceId = ResourceId ;
mCardNumber = CardNumber ;
mElement = Element ;
mColor = Color ;
}
public Card(String Element , String Color , String ResourceId , int CardNumber ){
mResId = ResourceId ;
mCardNumber = CardNumber ;
mElement = Element ;
mColor = Color ;
}
public Card(String Element , String Color , int ResourceId , int CardNumber ,String Special){
mResourceId = ResourceId ;
mCardNumber = CardNumber ;
mElement = Element ;
mColor = Color ;
mSpecial=Special;
}
protected Card(Parcel in) {
mResourceId = in.readInt();
mCardNumber = in.readInt();
mColor = in.readString();
mResId = in.readString();
mElement = in.readString();
mSpecial = in.readString();
}
public static final Creator<Card> CREATOR = new Creator<Card>() {
@Override
public Card createFromParcel(Parcel in) {
return new Card(in);
}
@Override
public Card[] newArray(int size) {
return new Card[size];
}
};
public Object getResourceId() {
if (mResId==null){return mResourceId;}
else {return mResId;}
}
public int getResourceId(Context context) {
try {
return context.getResources().getIdentifier(mResId, "drawable", context.getPackageName());
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public void setResourceId(int ResourceId) {this.mResourceId = ResourceId;}
public void setElement(String Element) {this.mElement = Element;}
public void setCardNumber(int CardNumber) {this.mCardNumber = CardNumber;}
public int getCardNumber() {
return mCardNumber;
}
public String getCardElement() {
return mElement;
}
public String getCardColor() {
return mColor;
}
public String getSpecial() {
return mSpecial;
}
public boolean isPowerCard(){
boolean isspecial=false;
if (mSpecial==null){isspecial= false;}
else {isspecial= true;}
return isspecial;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mResourceId);
dest.writeInt(mCardNumber);
dest.writeString(mColor);
dest.writeString(mElement);
dest.writeString(mSpecial);
}
}
答案 0 :(得分:3)
资源ID是随着应用程序更新而动态生成的,因此在您的情况下失败了。
您可以尝试以共享首选项存储资源的名称,并使用该名称获取资源的ID。
Resources resources = getResources();
String resourceName = resources.getResourceEntryName(R.drawable.resource);
然后您可以从共享首选项中检索ID。
intresId = resources.getIdentifier(resourceName, "drawable", "your.package.name");