passing arrays using bundle in android

后端 未结 5 1772
没有蜡笔的小新
没有蜡笔的小新 2020-12-28 19:19

I need to pass an array of String/integer values from one Activity to another. How do I achieve this?

5条回答
  •  醉话见心
    2020-12-28 20:02

    At the sender side, the code should be:

    String[] myStrings=new String[2];
    myStrings[0]="MONDAY";
    myStrings[1]="TUESDAY";
    Intent intent = new Intent(v.getContext(), Animation_program.class);
    Bundle bundle = new Bundle();
    intent.putExtra("strings", myStrings);
    intent.putExtras(bundle);               
    startActivity(intent);
    

    At the reciever side, the code should be:

    Intent i = getIntent();
    Bundle extras=i.getExtras();
    
    if(extras != null)  //this line is necessary for getting any value
    {
        String[] fajr_Values = i.getStringArrayExtra("strings");
        Toast.makeText(this, "value="+fajr_Values[0]+""+fajr_Values[1], Toast.LENGTH_SHORT).show();
    }
    

提交回复
热议问题