I have a bidimensional String array that has mass data (sizes are [40][8] to be exact) in my Activity A. I want to pass the whole array to Activity B. I've tried this solution: Passing string array between android activities
but the problem is it is creating an array inside. I want to pass my original bidimensional array that contains [40][8] values. Can I do that?
EDIT:
my String array is a 2 dimensional array.
Since your array is bidimensional and bidimensional array are serializable, you can use a bundle:
Intent intent = new Intent(this, activityB.class);
Bundle bundle = new Bundle();
bundle.putSerializable("myArray", myBidimensionalArray);
intent.putExtras(bundle);
and in activityB you can simply call:
Intent passed = getIntent();
Bundle bundle = passed.getExtras();
String[][] myPassedArray = (String[][]) bindle.getSerializable("myArray");
and you are done
Related
I have a sign-up form, and I have to pass all info filled in that form to another screen. I know how to display if there is one field, but in sign-up form there are multiple fields. so I want to know how to display all the info.
If you are launching a new activity, just create a Bundle, add your values, and pass it into the new activity by attaching it to the Intent you are using:
/*
* In your first Activity:
*/
String value = "something you want to pass along";
String anotherValue = "another something you would like to pass along";
Bundle bundle = new Bundle();
bundle.putString("value", value);
bundle.putString("another value", anotherValue);
// create your intent
intent.putExtra(bundle);
startActivity(intent);
/*
* Then in your second activity:
*/
Bundle bundle = this.getIntent().getExtras();
String value = bundle.getString("value");
String anotherValue = bundle.getString("another value");
To pass User data(multiple info) from one screen to another screen :
Create a model for user with setter and getter method.
make this class Serializable or Parcelable (Prefer) .
Create object of user class and set all data using setter method.
Pass this object from one activity to another by using putSerializable.
Person mPerson = new Person();
mPerson.setAge(25);
Intent mIntent = new Intent(Activity1.this, Activity2.class);
Bundle mBundle = new Bundle();
mBundle.putSerializable(SER_KEY,mPerson);
mIntent.putExtras(mBundle);
startActivity(mIntent);
And get this object from activity 2 in on create methode.
Person mPerson = (Person)getIntent().getSerializableExtra(SER_KEY);
and SER_KEY will be same.
for more detail please go to this link:
http://www.easyinfogeek.com/2014/01/android-tutorial-two-methods-of-passing.html
I hope it will work for you.
You can make use of bundle for passing values from one screen to other
Passing a Bundle on startActivity()?
I have two activities in my tab-bar application. One activity (activity1) is a normal activity and the other (activity2) is a ListActivity. In activity1, there is a button which stores every time I tap on the button data (date, value1,value2..) in an array of objects. I read that this is possible with Intents. But I dont want to start activity2 everytime I tap on the button.
How can I pass the array to activity2, so I can display them later after I tapped more than once on the button in my ListView?
EDIT :
Here is my code. But I get a NullPointerException, so it doesn't store correctly. My Object hat Serializable implemented.
Activity1:
Intent datenIntent = new Intent(this, LoggerActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("Daten",aDatensatz);
datenIntent.putExtras(bundle);
Activity2:
Bundle bundleData = this.getIntent().getExtras();
Datensatz[] aDatensatz = (Datensatz[]) bundleData.getSerializable("Daten");
You can pass an array in one shot from one activity to another in the following way :
Bundle myBundle = new Bundle();
b.putStringArray(key, array);
Intent i = new Intent(context, Class);
i.putExtras(myBundle);
And for receiveing
You can retrieve the value in another activity in following way:
Bundle b = this.getIntent().getExtras();
String[] array = b.getStringArray(key);
Hope this helps.
I have two activities say X and y. In x there is edittext n 6 radiobutton if user clicks button the value gets retrieved from database based on input from edittext n radiobutton. the values should be displayed in next activity y. can yu pls help in giving snippet..thanks in advance
You can easily pass data from one activity to another using Bundle or Intent.
Lets look at the following example using Bundle:
//creating an intent to call the next activity
Intent i = new Intent("com.example.NextActivity");
Bundle b = new Bundle();
//This is where we put the data, you can basically pass any
//type of data, whether its string, int, array, object
//In this example we put a string
//The param would be a Key and Value, Key would be "Name"
//value would be "John"
b.putString("Name", "John");
//we put the bundle to the Intent
i.putExtra(b);
startActivity(i, 0);
In "NextActivity" you can retrieve the data using the following code:
Bundle b = getIntent().getExtra();
//you retrieve the data using the Key, which is "Name" in our case
String data = b.getString("Name");
How about using only Intent to transfer data. Lets look at the example
Intent i = new Intent("com.example.NextActivity");
int highestScore = 405;
i.putExtra("score", highestScore);
In "NextActivity" you can retrieve the data:
int highestScore = getIntent().getIntExtra("score");
Now you would ask me, whats the difference between Intent and Bundle, they seems like
they do exactly the same thing.
The answer is yes, they both do exactly the same thing. But if you want to transfer alot of data, variables, large array you would need to use Bundle, as they have more methods for transferring large amount of data.(i.e. If your only passing one or two variable then just go with Intent.
You should put the values into a bundle and pass that bundle to the intent that's starting the next activity. Sample code is in the answer to this question: Passing a Bundle on startActivity()?
Bind the data that you would like to send with the Intent you are calling to go to the next activity.
Intent i = new Intent(this, YourNextClass.class);
i.putExtra("yourKey", "yourKeyValue");
startActivity(i);
In YourNextClass activity, you can get the passed data by using
Bundle extras = getIntent().getExtras();
if (extras != null) {
String data = extras.getString("yourKey");
}
I have a problem to pass an integer array between two activities. I tried this code:
ActivityA:
Bundle myBundle = new Bundle();
myBundle.putIntArray("myarray", array);
startActivity(intent);
ActivityB:
Bundle myBundle = getIntent().getExtras();
int[] myIntArray = myBundle.getIntArray("myarray");
I don't understand why i can't use array values in activityB.
Can you help me, please?
Thanks
Activity B:
Bundle bundle = getIntent().getExtras();
the do your code.
You are never adding the bundle to the Intent in Activity A. In addition, since you are passing an array of integers, you can add them directly to the intent. Like this:
intent.putExtra("myarray", array);
startActivity(intent);
Then you can easily retrieve it in Activity B like this:
int[] myIntArray = getIntent().getIntArrayExtra("myarray");
I'm trying to pass a String array from one activity to another but when I try to read the array in the second activity, the values are null.
Here is how i'm passing the array from the first activity:
Bundle bundle = new Bundle();
bundle.putStringArray("Array", createArray(text));
Intent itemIntent = new Intent(this,Details.class);
itemIntent.putExtra("passedArray", bundle);
startActivity(itemIntent);
createArray(text) is a method that returns an array.
Here's how i'm trying to read the array in the second activity:
Bundle extras = this.getIntent().getExtras();
String[] array = extras.getStringArray("Array");
How do I initialise the array in the second activity with the corresponding array values that have been passed to it? If I try and read any of the values they have not been initialised and are null.
Don't use bundle try this and first calculate your array:
itemIntent.putExtra("passedArray", createArray(text));
startActivity(itemIntent);
And receive them as
String[] array = this.getIntent().getStringArray("passedArray");
I think you are messing around with bundle and extras. Look at this question. I think it can help you
Sending arrays with Intent.putExtra
You are nesting the bundle containing your array within another extra.
That means you'd have to get the "passedArray" extra (which is a bundle) and then get your "Array" from the bundle that you just extracted. Instead change your code to this
Intent itemIntent = new Intent(this,Details.class);
itemIntent.putExtra("Array", createArray(text));
startActivity(itemIntent);