簡體   English   中英

使用Intent.putExtra發送數組

[英]Sending arrays with 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'。 我一直在尋找一些例子,但我沒有發現任何事情。

您正在使用數組設置額外的。 然后你試圖得到一個int。

你的代碼應該是:

int[] arrayB = extras.getIntArray("numbers");

此代碼發送整數值數組

初始化數組列表

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");
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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM