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
Related
I need to pass a 2D array as an intent onto another activity.
This code works for sending the 2D array, the problem is retrieving it in the next activity.
i++;
Intent tryAgain = getIntent();
tryAgain.putExtra("index", i);
tryAgain.putExtra("from", "next");
int [][] previousValues = getSelectedIndices();
Toast.makeText(AnotherActivity.this, "Bang " + previousValues[3][3], Toast.LENGTH_SHORT).show();
Bundle mBundle = new Bundle();
mBundle.putSerializable("array", previousValues);
tryAgain.putExtras(mBundle);
AnotherActivity.this.finish();
startActivity(tryAgain);
I have already tried this code below to retrieve the 2D array, it does NOT work.
else if(getIntent().getStringExtra("from").equals("next")){
i=index;
int [][] arrayReceived=null;
Object[] objectArray = (Object[]) getIntent().getExtras().getSerializable("array");
if(objectArray!=null){
arrayReceived = new int[objectArray.length][];
for(int i=0;i<objectArray.length;i++){
arrayReceived[i]=(int[]) objectArray[i];
}
}
You can do something for receiving array like this as an example:
Bundle b = getIntent().getExtras();
int[][] list_array = (int[][])b.getSerializable("array");
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);
I am trying to pass value "0000002" in string format to next activity like below:
Intent pass = new Intent(this, SecondActivity.class);
pass.putExtras("EmpID", "0000002");
In second activity
Bundle info = getIntent().getExtras();
System.out.println("Test " + info.getString("EmpID")); // this line printing "null" value instead of "0000002".
I am able to pass and fetch the other strings successfully. I am not able to fetch the EmpID.
Please help me.
Here is a Sample
From 1st Activity
Bundle localBundle = new Bundle();
localBundle.putString("Loan Amount", editText1.getText().toString());
localBundle.putString("Loan Tenture", editText2.getText().toString());
localBundle.putString("Interest Rate", editText3.getText().toString());
Intent localIntent = new Intent(this, Activity2.class);
localIntent.putExtras(localBundle);
startActivity(localIntent);
and in Activity2
String string1 = getIntent().getStringExtra("Loan Amount");
String string2 = getIntent().getStringExtra("Loan Tenture");
String string3 = getIntent().getStringExtra("Interest Rate");
For your case, you can use like
Bundle localBundle = new Bundle();
localBundle.putString("EmpID", "0000002");
Intent pass = new Intent(this, SecondActivity.class);
pass.putExtras(localBundle);
startActivity(pass);
and in SecondActivity you can get the EmpId like
String empId = getIntent().getStringExtra("EmpID");
----------------- Another Way -----------------
Intent pass = new Intent(this, SecondActivity.class);
pass.putExtra("EmpID", "0000002");
startActivity(pass);
In second activity you can get the EmpId like
Bundle bundle = getIntent().getExtras();
String empId = bundle.getString("EmpID");
Write pass.putExtra("EmpID", "0000002"); not putExtras
use this
Intent pass = new Intent(this, SecondActivity.class);
pass.putExtra("EmpID", "0000002");
startActivity(pass);
In second activity
Bundle info = getIntent().getExtras();
System.out.println("Test " + info.getString("EmpID"));
//Activity A
Intent pass = new Intent(this, SecondActivity.class);
pass.putExtra("EmpID", "0000002");
//Activity B
Intent intent = getIntent();
String EmpID = intent.getStringExtra("EmpID");
System.out.println("Test " + EmpID);
Try Using this:
When passing value:
Intent pass = new Intent(this, SecondActivity.class);
pass.putExtra("EmpID", "0000002");
To get the value:
System.out.println("Test " + getIntent().getStringExtra("EmpID"));
In my app, i need to send a Two dimensional Array and two more integer values form on Activity to Another with the help of Intent method.
This is done as ..
Intent i = new Intent(getApplicationContext(), ViewActivity.class);
Bundle postbundle = new Bundle();
String[][] X={{"abc"},{"def"}};
postbundle.putSerializable("data", X);
i.putExtra("A", postbundle);
i.putExtra("albumid", position);
i.putExtra("Bigcard",bigcard);
here am using .putSerializable method to place an array into bundle.
So to access these data in the receiver Activity am using
Bundle bundle = getIntent().getBundleExtra("A");
String[][] ABC=(String[][]) bundle.getSerializable("data");
Log.e("Array is",""+ABC);
but I got java.lang.NullPointerException error message..
Whith out use of " Static " declaration how can i get these values from bundle here (in the receiver Activity..)
Let me out pls from this ..
step-1:Write a seperate bean class and save into another file
public class MyBean implements Serializable{
String[][] data = null;
public void set2DArray(String[][] data){
this.data = data;
}
public String[][] get2DArray(){
return data;
}
}
step-2:In the calling activity
Intent intent = new Intent(this, Second.class);
String data[][] = new String[][] {{"1","kumar"},{"2","sona"},{"3","kora"},{"1","pavan"},{"2","kumar"},{"3","kora333"}};
MyBean bean = new MyBean();
bean.set2DArray(data);
Bundle b = new Bundle();
b.putSerializable("mybean", bean);
intent.putExtra("obj", b);
startActivity(intent);
step-3:In the caller activity
Bundle b = getIntent().getBundleExtra("obj");
MyBean dData = (MyBean) b.getSerializable("mybean");
String[][] str =dData.get2DArray();
Not a real answer, but a try:
what happens if You try:
Intent intent = getIntent();
int a = intent.getIntExtra("albumid"); //if Your value is an int, otherwise use String
//getStringExtra or whatever Your value is
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);