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

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

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

GCT1015
TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個(gè)贊
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);}
添加回答
舉報(bào)
0/150
提交
取消