使用Intent.putExtra發送數組我在活動A中有一個整數數組:int array[] = {1,2,3};我想將該變量發送到活動B,因此我創建了一個新的intent并使用了putExtra方法:Intent i = new Intent(A.this, B.class);i.putExtra("numbers", array);startActivity(i);在活動BI中獲取信息:Bundle extras = getIntent().getExtras();int arrayB = extras.getInt("numbers");但這并不是真的發送數組,我只是在arrayB上得到值'0'。我一直在尋找一些例子,但我沒有發現任何事情。
3 回答

慕森卡
TA貢獻1806條經驗 獲得超8個贊
您正在使用數組設置額外的。然后你試圖得到一個int。
你的代碼應該是:
int[] arrayB = extras.getIntArray("numbers");

慕容3067478
TA貢獻1773條經驗 獲得超3個贊
此代碼發送整數值數組
初始化數組列表
List<Integer> test = new ArrayList<Integer>();
將值添加到數組列表
test.add(1);test.add(2);test.add(3);Intent intent=new Intent(this, targetActivty.class);
將數組列表值發送到目標活動
intent.putIntegerArrayListExtra("test", (ArrayList<Integer>) test);startActivity(intent);
在這里你可以獲得targetActivty的值
Intent intent=getIntent();ArrayList<String> test = intent.getStringArrayListExtra("test");

GCT1015
TA貢獻1827條經驗 獲得超4個贊
final static String EXTRA_MESSAGE = "edit.list.message";Context context;public void onClick (View view){ Intent intent = new Intent(this,display.class); RelativeLayout relativeLayout = (RelativeLayout) view.getParent(); TextView textView = (TextView) relativeLayout.findViewById(R.id.textView1); String message = textView.getText().toString(); intent.putExtra(EXTRA_MESSAGE,message); startActivity(intent);}
添加回答
舉報
0/150
提交
取消