我无法使用数组或捆绑包来传递意图

时间:2019-02-22 08:23:21

标签: android android-intent

如何通过参数传递意图?数组和捆绑软件均无效。

Intent intent = new Intent(Apply_leave_Activity.this, ApplyingReason_Activity.class);
intent.putExtra("ID_EXTRA", new String[] { "21",countOfCL,countOfSL ,countOfEL,startDatey,endDatey,currentDatey});
startActivity(intent);

接收代码

 String x[]=new String[10];
 x  =getIntent().getStringArrayExtra("ID_EXTRA");

使用捆绑包

Bundle extras = new Bundle();
  extras.putString("EID", "21");
  extras.putString("countOfCL", countOfCL);
  extras.putString("countOfSL", countOfSL);
  extras.putString("countOfEL", countOfEL);
  extras.putString("From_date", startDatey);
  extras.putString("To_date", endDatey);
  extras.putString("applied_date", currentDatey);
 intent.putExtras(extras);
 startActivity(intent);

接收方

Intent intent = getIntent();
Bundle extras = intent.getExtras();
      final   String EID = extras.getString("EID");
      final String numberOfCasualLeaves = extras.getString("countOfCL");
      final   String numberOfsickLeaves = extras.getString("countOfSL");
      final String numberOfearnedLeaves = extras.getString("countOfEL");
      final String from_date = extras.getString("From_date");
      final String to_Date = extras.getString("To_date");
      final String applied_date = extras.getString("applied_date");

1 个答案:

答案 0 :(得分:0)

首先像这样使ArrayList的String类型

 public ArrayList<String> getArrayList() {
    ArrayList<String> list = new ArrayList<>();
    list.add("21");
    list.add(countOfCL);
    list.add(countOfSL);
    list.add(countOfEL);
    list.add(startDatey);
    list.add(endDatey);
    list.add(currentDatey);

    return list;
}

之后,通过意图发送此ArrayList

    Intent intent = new Intent(this, ApplyingReason_Activity.class);
    intent.putStringArrayListExtra("key", getArrayList());
    startActivity(intent);

像这样在另一项活动中捕捉它

     Intent i = getIntent();
    ArrayList<String> list = i.getStringArrayListExtra("key");

之后,您可以从ArrayList中获取值