Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing arrays using bundle in android

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

like image 474
chetan Avatar asked Mar 14 '11 13:03

chetan


4 Answers

In activity A:

String[] abc;

Bundle bundle =new Bundle();
bundle.putStringArray("some string",abc);

In Activity B where you want to get give the code as:

String abcd[]=bundle.getStringArray("some string");

"some string" should be same in both case.

like image 97
Niki Avatar answered Nov 10 '22 19:11

Niki


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();
}
like image 31
PIR FAHIM SHAH Avatar answered Nov 10 '22 18:11

PIR FAHIM SHAH


I have never passed an array using a bundle, and I do not know off the top of my head if it can be done, but you can certainly pass an ArrayList (or anything Serializable/Parcelable). See this question for a more complete answer:

Passing data of a non-primitive type between activities in android

like image 2
Cephron Avatar answered Nov 10 '22 17:11

Cephron


Refer this pass arraylist from one activity to other may help you

like image 1
Labeeb Panampullan Avatar answered Nov 10 '22 19:11

Labeeb Panampullan