how to send the array by using an intent? [duplicate] - android

This question already has answers here:
Sending arrays with Intent.putExtra
(3 answers)
Closed 5 years ago.
how to send the array in intent in android studio?
i have two array list
Eve_id[];//here i had stored 5 diffrent values.
sc_id[];//here i had stored 5 diffrent values.
now i have to send these two array to next activity,i have used the code like this
1st activity`
Intent k = new Intent(context, Receiver.class);
k.putExtra("Event_id", Eve_id);
k.putExtra("schedule_id", sc_id);`
2nd activity
Bundle extras = getIntent().getExtras();
long[] event_id = extras.getLongArray("Event_id");
int[] schedule_id = extras.getIntArray("schedule_ids");
i dont know weather it is correct are not ,but i am not able to receive any data!
any one can suggest how to solve this!

You can use Bundle for That
Intent passArray = new Intent(MainActivity.this ,YourSecondClass.class);
Bundle bundle = new Bundle();
bundle.putStringArray("my_array", array);
startActivity(passArray , bundle);
passArray.putExtras(bundle);
startActivity(passArray);

Use ArrayList instead of array:
ArrayList<Long> eventIds = new ArrayList<>();
ArrayList<Integer> scheduleIds = new ArrayList<>();
fill them and put into bundle:
Bundle extras = new Bundle();
extras.putIntegerArrayList("schedule_ids",scheduleIds);
extrass.putLongArrayList("Event_id", eventIds);
Intent k = new Intent(context, Receiver.class);
k.putExtras(extras);
and then extract this values in Receiver activity:
Bundle extras = getIntent().getExtras();
ArrayList<Long> eventIds = extras.getLongArrayList("Event_id");
ArrayList<Integer> scheduleIds = extras.getIntegerArrayList("schedule_ids");

Related

How to get this type of array from string.xml file to an android activity [duplicate]

This question already has answers here:
how we can get the arraylist(both string and integer) from the resources xml
(5 answers)
Closed 4 years ago.
photo of the integer array in string.xml
I want to access this integer array into an arraylist in activity.
You can access this array as following:
int[] photos = context.getResources().getIntArray(R.array.photo);
You should use
Int[] intarray = getResources().getIntArray(R.string.photo);
I'm not sure if you are asking how to pass this to an Intent or how to convert it to ArrayList.
If it is the former:
1) Use the Bundle from the Intent:
Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);
2) Create a new Bundle
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);
If you are asking about the latter try:
Resources r = getResources();
int[] p = r.getIntArray(R.array.photo);
Solution:
Create an Array-List of Integers:
public ArrayList<Integer> photoList = new ArrayList<>();
Then,
photoList = Arrays.asList(context.getResources().getIntArray(R.array.photo));
This will give you as ArrayList
Hope it helps.

Pass ArrayList of integer array via bundle

I'm looking for a way to pass, via bundle, an
ArrayList<Integer[]>
object to a Fragment (after activity is already created, so i can't use intent). By looking to android api, no method seems to do what i'm looking for. How can i do?
Sending activity:
final Intent intent = new Intent(this, SecondActivity.class);
Bundle extraBundle = new Bundle();
extraBundle.putIntegerArrayList("arraylist", [put array list here]);
intent.putExtras(extraBundle);
intent.setComponent(new ComponentName("com.myapp", "com.myapp.SecondActivity"));
startActivity(intent);
Receiving activity's onCreate():
final Intent intent = getIntent();
final Bundle extraBundle = intent.getExtras();
ArrayList<Integer> myIntegerArrayList = extraBundle.getIntegerArrayList("arraylist");
You can change "arraylist" to what you want in the setter and getter method calls, they just need to be the same.
I used Gson, convert to JSONArray and send via Bundle. But it may affects performance.
In First Activity
Intent activity = new Intent(MyActivity.this,NextActivity.class);
activity.putExtra("myArrayList", new Gson().toJson(myArrayList);
startActivity(activity);
In other activity..
Sting myArrayList;
Bundle extras = getIntent().getExtras();
if (extras != null) {
myArrayList= extras.getString("myArrayList");
Type listOfInteger = new TypeToken<List<Integer>>(){}.getType();
String s = new Gson().toJson(myArrayList, listOfInteger);
List<Integer> myList = new Gson().fromJson(s, listOfInteger);
}
int[] intarray = new int[] {4,5,6,7,8};
Bundle bundle = new Bundle();
bundle.putIntArray("integerarray", intarray);
Or an ArrayList with int arrays?
For that i think you should use Parcable.
Or you could try to send the individual intArrays via the bundle and put them together in the receiving Activity.
Like this:
int[] intarray1 = new int[] {4,5,6,7,8};
int[] intarray2 = new int[] {4,5,6,7,8};
int[] intarray3 = new int[] {4,5,6,7,8};
Bundle bundle = new Bundle();
bundle.putIntArray("INT_ARRAY1", intarray1);
bundle.putIntArray("INT_ARRAY2", intarray2);
bundle.putIntArray("INT_ARRAY3", intarray3);
Intent intent = new Intent(this,NewActivity.class)
intent.putExtras(bundle);
startActivity(intent)
Then in NewActivity.java you should create an array and get the bundle and fill the array with the received arrays.
you can pass the Array List of integers by using the method putIntegerArrayList
Here is the code snippet
ArrayList<Integer> values=new ArrayList<>();
values.add(1);
values.add(60);
values.add(75);
values.add(120);
Bundle extras = new Bundle();
extras.putIntegerArrayList(EXTRA_KEY,values);

passing intent between for two string arrays

I have two string Arrays, contactNames in the main Activity class, and address [] in the second activity class. when i click on the one of the names, it should show me the address details for that name. How should I create an intent for two string arrays in Android.
class ContactList
Intent i = new Intent (MainActivity.this, Details.class);
Bundle bundle = new Bundle ();
bundle.putStringArray("key", address);
i.putExtras(bundle);
startActivity(i);
}
class Details
Bundle b = this.getIntent().getExtras();
String array [] = b.getStringArray("key");
tvEmail.setText(array.toString());
}
thanks.
How should I create an intent for two string arrays in Android.
You can add another array (or any other data type) but you need to use a different key name. Example...
String[] array1;
String[] array2;
// Populate arrays here
Intent i = new Intent (MainActivity.this, Details.class);
Bundle bundle = new Bundle ();
bundle.putStringArray("key1", array1);
bundle.putStringArray("key2", array2);
i.putExtras(bundle);
startActivity(i);
Also note that you don't need to use a Bundle directly. You can use the Intent method putExtra(String name, String[] value). For example...
String[] array1;
String[] array2;
// Populate arrays here
Intent i = new Intent (MainActivity.this, Details.class);
i.putExtra("key1", array1);
i.putExtra("key2", array2);
startActivity(i);

passing 2d array to a new activity in android

How to pass a 2d String array to new activity???
and then in new activity, How can i retrieve the array??
You have to use:
String [][]str;
Intent summaryIntent = new Intent(this, Second.class);
Bundle b=new Bundle();
b.putSerializable("Array", str);
summaryIntent.putExtras(b);
startActivity(summaryIntent);
For Recieving the Array use:
Bundle b = getIntent().getExtras();
String[][] list_array = (String[][])b.getSerializable("Array");
Thanks

problems with putStringArrayListExtra() in Android

Hi
I want to pass an Arraylist from one activity to another.
I use putStringArrayListExtra(), but there shows an error :
"The method putStringArrayListExtra(String,ArrayList is undefined for the type bundle."
Is there any other method available for passing ArrayList?
String test[]=new String[3];
ArrayList<String[]> al=new ArrayList<String[]>();
int x,y;
test[0]="1";
test[1]="2";
test[2]="3";
al.add(test);
test = new String[3];
test[0]="4";
test[1]="5";
test[2]="6";
al.add(test);
Bundle list_bundle=new Bundle();
list_bundle.putStringArrayListExtra("lists",al);
Intent list_intent= new Intent(v.getContext(), view_all_selected.class);
list_intent.putExtras(list_bundle);
startActivityForResult(list_intent, 2);
Please help me..
putStringArrayListExtra is a method of Intent class. In the code above try to call:
list_intent.putStringArrayListExtra("lists",al);
and remove these lines:
Bundle list_bundle=new Bundle();
list_bundle.putStringArrayListExtra("lists",al);
list_intent.putExtras(list_bundle);

Categories

Resources