可分配更改Android上的资源ID

时间:2016-09-11 17:10:11

标签: android android-activity android-resources parcelable

我有Product类,它实现了Parcelable。这个类有一些图像,这些图像路径保存在strings.xml中。我有产品对象到另一个活动,但图像资源更改。所以,我得到ResourcesNotFoundExceptions。

这是我的strings.xml

 <string-array name="prod_images">
    <item>@drawable/sample_product_one</item>
    <item>@drawable/sample_product_two</item>
    <item>@drawable/sample_product_three</item>
    <item>@drawable/sample_product_four</item>
</string-array>

我的Product.java类。

public class Product implements Parcelable {

private int productId;
private int productImage;
private String productTitle;
private String productPrice;

public Product() {
}

public int getProductImage() {
    return productImage;
}

public void setProductImage(int productImage) {
    this.productImage = productImage;
}

public String getProductTitle() {
    return productTitle;
}

public void setProductTitle(String productTitle) {
    this.productTitle = productTitle;
}

public String getProductPrice() {
    return productPrice;
}

public void setProductPrice(String productPrice) {
    this.productPrice = productPrice;
}

public int getProductId() {
    return productId;
}

public void setProductId(int productId) {
    this.productId = productId;
}

// Parcelling part
public Product(Parcel in){
    this.productTitle = in.readString();
    this.productPrice = in.readString();
    this.productId = in.readInt();
    this.productImage = in.readInt();
}


@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(this.productId);
    dest.writeInt(this.productImage);
    dest.writeString(this.productTitle);
    dest.writeString(this.productPrice);
}

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
    public Product createFromParcel(Parcel in) {
        return new Product(in);
    }

    public Product[] newArray(int size) {
        return new Product[size];
    }
};
}

我使用typedarray来获取图像资源。

private void getProductResources() {
    typedArrayImages = getActivity().getResources().obtainTypedArray(R.array.prod_images);
    prodTitles = getActivity().getResources().getStringArray(R.array.prod_titles);
    prodPrices = getActivity().getResources().getStringArray(R.array.prod_prices);
}

private void getProductList() {
    for (int i = 0; i < 4; i++) {
        int rand_index = random.nextInt(4);
        Product product = new Product();
        product.setProductImage(typedArrayImages.getResourceId(rand_index, -1));
        product.setProductPrice(prodPrices[rand_index]);
        product.setProductTitle(prodTitles[rand_index]);
        product.setProductId(prodIds[rand_index]);
        this.product.add(product);
    }
    typedArrayImages.recycle();
    mainPageAdapter.notifyDataSetChanged();
}

我点击了recyclerview上的图片,获得了另一个带有parcelable对象的活动。

        mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), mRecyclerView, new RecyclerTouchListener.ClickListener() {
        @Override
        public void onClick(View view, int position) {
            if(position != 0){
                Intent i = new Intent();
                Bundle b = new Bundle();
                b.putParcelable("Product", product.get(position - 1));
                i.putExtras(b);
                i.setClass(getActivity(), ProductActivity.class);
                Log.i("PRODUCT", product.get(position - 1).getProductImage() + "");
                startActivity(i);
            }
        }

我在set image资源上有一个ResourceNotFoundException。我可以在单击图像之前和之后记录资源图像。图像资源发生变化。

    productTitle.setText(product.getProductTitle());
    productCost.setText(product.getProductPrice());
    //Different image resources
    Log.i("PRODUCT", product.getProductImage() + "");
    //ResourcesNotFoundExceptions
    productImg.setImageResource(product.getProductImage());

1 个答案:

答案 0 :(得分:6)

您必须以相同的顺序阅读和书写。

你写了两个整数,然后是两个字符串

dest.writeInt(this.productId);
dest.writeInt(this.productImage);
dest.writeString(this.productTitle);
dest.writeString(this.productPrice);

但你读了两个字符串,然后是两个整数

this.productTitle = in.readString();
this.productPrice = in.readString();
this.productId = in.readInt();
this.productImage = in.readInt();