我刚刚开始学习java。我正在尝试将可绘制图像的 int 数组从一个类传输到另一个类。这是我尝试过的:
第一类如下
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);
当我运行它时,图像没有出现在目标类中。有什么解决办法吗?
最佳答案
试试这个您可以使用 Intent 发送和读取整数数组。
来源 Activity
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);
目的地 Activity
Bundle extras = getIntent().getExtras();
int[] myDrawableArray = extras.getIntArray("myDrawableArray");
使用数组中的图像:
ImageView iv = (ImageView) findViewById(R.id.himg);
iv.setImageResource(myDrawableArray[2]);
我使用了数组的静态索引,您可以根据要求通过 Intent 传递索引。
关于java - 如何将可绘制的 int 数组从一个类传输到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38864082/