我需要使用最初从另一个活动传递的ImageView资源文件

时间:2018-02-20 00:31:35

标签: android xml

在主要活动

上填写imageview onClick事件的代码
if (view.equals(findViewById(R.id.madrid_imageButton))) {
            Bitmap madridImage = BitmapFactory.decodeResource(getResources(),R.drawable.madrid);
            Intent buyProduct_Intent = new Intent(MainActivity.this,MainActivityBuy.class);
            buyProduct_Intent.putExtra("Flavor","madrid");
            buyProduct_Intent.putExtra("Description",madrid_Description);
            buyProduct_Intent.putExtra("Bitmap",madridImage);
            startActivity(buyProduct_Intent);

        } else if (view.equals(findViewById(R.id.berlin_imageButton))) {
            Bitmap berlinImage = BitmapFactory.decodeResource(getResources(),R.drawable.berlin);
            Intent buyProduct_Intent = new Intent(MainActivity.this,MainActivityBuy.class);
            buyProduct_Intent.putExtra("Flavor","berlin");
            buyProduct_Intent.putExtra("Description",berlin_Description);
            buyProduct_Intent.putExtra("Bitmap",berlinImage);
            startActivity(buyProduct_Intent);

我有上面的10个检查,这里是在上述条件

之后传递的新Activity的代码
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.buy_product); // Load buy product screen

        TextView productNameTextView = (TextView) findViewById(R.id.productName_textView);
        TextView productDesciptionTextView = (TextView) findViewById(R.id.productDescription_textView);
        ImageView productImageView = (ImageView) findViewById(R.id.productImage_imageView);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            productNameTextView.setText(extras.getString("Flavor"));
            productDesciptionTextView.setText((extras.getString("Description")));
            Bitmap sharmImage = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");
            productImageView.setImageBitmap(sharmImage);
        }

        findViewById(R.id.size_radioButton_60ml).performClick();
        findViewById(R.id.nic_radioButton_3mg).performClick();
        findViewById(R.id.size_radioButton_30ml).performClick();
    };

从第二个活动开始,我需要尝试使用某些视图值的第三个活动。我成功地检索了所有需要的值,除了它显示为空的imageView不知道为什么.....

这是代码包含问题

的第三个活动的代码
public void onClickAddToCart(View view) {
            TextView orderDetailsTextView1 = (TextView) findViewById(R.id.productName_textView);
            TextView orderDetailsTextView2 = (TextView) findViewById(R.id.totalItems_textView);
            TextView orderDetailsTextView3 = (TextView) findViewById(R.id.totalCost_textView);
            String orderDetailsTextView = orderDetailsTextView1.getText().toString() + "  " +  orderDetailsTextView2.getText().toString() + "  " + orderDetailsTextView3.getText().toString();

            ImageView productImageView = (ImageView) findViewById(R.id.productImage_imageView);
            ImageView newImageView = new ImageView(this);
            newImageView.setImageDrawable(productImageView.getDrawable());

            int drawableId = getResources().getIdentifier(productImageView.getDrawable().toString(), "drawable", getPackageName());
            Bitmap productImage = BitmapFactory.decodeResource(getResources(),drawableId);

            Intent buyProduct_Intent = new Intent(MainActivityBuy.this, Cart.class);
            buyProduct_Intent.putExtra("OrderDetails", orderDetailsTextView);
            buyProduct_Intent.putExtra("Bitmap", newImageView.getDrawable().toString());
            startActivity(buyProduct_Intent);
        }

以下是第三项活动的onCreate的代码

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.product_cart); // Load the cart XML Screen

        TextView orderDetailsTextView = (TextView) findViewById(R.id.orderDetailsTextView);
        ImageView productImageView = (ImageView) findViewById(R.id.productImage_imageView);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            orderDetailsTextView.setText(extras.getString("OrderDetails"));
            Bitmap productImage = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");
            productImageView.setImageBitmap(productImage);
        }
    }

    @Override
    public void onBackPressed() {
//        startActivity(new Intent(MainActivityBuy.this, MainActivity.class));
        finish();
    }

}

顺便说一下,没有错误或崩溃..只有第三个活动的空白imageview,但前两个活动运行正常,XML也很好..

1 个答案:

答案 0 :(得分:0)

基于上述问题和评论期间的头脑风暴,我提出了以下想法,它实际上解决了问题:)

public void onClickAddToCart(View view) {
        // Here is i concatenated three textViews values - calculations into one single string 
        // to pass it to the Cart activity
        TextView orderDetailsTextView1 = (TextView) findViewById(R.id.productName_textView);
        TextView orderDetailsTextView2 = (TextView) findViewById(R.id.totalItems_textView);
        TextView orderDetailsTextView3 = (TextView) findViewById(R.id.totalCost_textView);
        String orderDetailsTextView = orderDetailsTextView1.getText().toString();
        orderDetailsTextView +=   "  " +  orderDetailsTextView2.getText().toString();
        orderDetailsTextView +=  "  :  " + orderDetailsTextView3.getText().toString();

        // in the below declaration i saved the originally intent image source to a variable 
        // and her i recall it to re-intent it again
        Bundle extras = getIntent().getExtras();
        Bitmap productImage = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");

        // Here is the passing method - transfering below data from this activity to the Cart activity
        Intent buyProduct_Intent = new Intent(MainActivityBuy.this, Cart.class);
        buyProduct_Intent.putExtra("OrderDetails", orderDetailsTextView);
        buyProduct_Intent.putExtra("Bitmap", productImage);
        startActivity(buyProduct_Intent);
    }