2016-08-10 58 views
1

我刚开始学习java。我正在尝试将一个int类型的可绘制图像从一个类传递到另一个类。这是我的尝试:如何将int数组的drawable从一个类传递到另一个类

第一类如下

Intent intent = new Intent(context, target.class); 
intent.putExtra("symbols", symbols); 
      context.startActivity(intent); 

目标类如下

int symbols = getIntent().getIntExtra("symbols", 0); 

    ImageView iv = (ImageView) findViewById(R.id.himg); 
    iv.setImageResource(symbols); 

,当我跑了,但图像没有目标类中露面。任何解决方案

+1

[使用android-intent传递数组]可能的副本(http://stackoverflow.com/questions/27215000/passing-an-array-with-android-intent) –

回答

1

尝试此操作您可以发送&使用intent读取整数数组。

来源活动

int myDrawableArray[] = {R.drawable.icon_home,R.drawable.icon_search,R.drawable.icon_plus}; 
Intent switchIntent = new Intent(A.this, B.class); 
switchIntent.putExtra("myDrawableArray", myDrawableArray); 
startActivity(switchIntent); 

目的地活动从阵列

Bundle extras = getIntent().getExtras(); 
int[] myDrawableArray = extras.getIntArray("myDrawableArray"); 

图像用途:

ImageView iv = (ImageView) findViewById(R.id.himg); 
iv.setImageResource(myDrawableArray[2]); 

我已经使用数组的静态索引,你可以按照需求通过意图传递索引。

相关问题