pass array of values between two activities - android

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");

Related

Passing bidimensional String Array variable from 1 activity to another

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

Android: How to pass MULTIPLE ArrayList<String> values and one ArrayList<LatLng> from one activity to another in a bundle? Not working

I have several List<String> variables to pass from Splash to the Main activity:
1) I read somewhere that I can pass them as ArrayList<String> from Splash to Main, and it works... i.e.
I can receive only the first ArrayList<String> variable. In my bundle below, I am not able to receive the second ArrayList<String>. (array_list2) Why?
2) How to pass ArrayList<LatLng> from one activity to another
First Activity:
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
Bundle b=new Bundle();
b.putStringArrayList("array_list1",(ArrayList<String>)Names);
b.putStringArrayList("array_list2",(ArrayList<String>)City);
b.putStringArrayList("array_list3",(ArrayList<String>)Country);
b.putStringArrayList("array_list4",(ArrayList<String>)Code);
b.putStringArrayList("array_list5",(ArrayList<LatLng>)coordinates); //ERROR in this line, type mismatch!
intent.putExtras(b);
startActivity(intent);
Second Activity:
Bundle b = getIntent().getExtras();
if (b != null) {
testList1 = b.getStringArrayList("array_list1");
testList2 = b.getStringArrayList("array_list2"); //THIS gives the same arraylist as testList1 and it is incorrect!
Log.e("TESTLIST1",testList1.toString()); //just using Log.e to view o/p as test
Log.e("TESTLIST2",testList1.toString());
Please answer both my questions. None of the other topics helped me, and I spent over 2 hours on this.
Thank you.
putStringArrayList() will not support for ArrayList .
use
putParcelableArrayList();
instead of
putStingArrayList();
or else you can use direct method of intent.
Change your code somethng like this:
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
Bundle b=new Bundle();
b.putStringArrayList("array_list1",(ArrayList<String>)Names);
b.putStringArrayList("array_list2",(ArrayList<String>)City);
b.putStringArrayList("array_list3",(ArrayList<String>)Country);
b.putStringArrayList("array_list4",(ArrayList<String>)Code);
If you want to pass through bundle object use below line of code.
b.putParcelableArrayList("array_list5",(ArrayList<LatLng>)coordinates);
Otherwise pass through intent object.
intent.putParcelableArrayListExtra("array_list5",coordinates);
intent.putExtras(b);
startActivity(intent);
Because LatLng class implements Parcelable interface, so instead of using Bundle. putStringArrayList use Bundle.putParcelableArrayList to send ArrayList which contains class object which is implementing Parcelable interface. Use
b.putParcelableArrayList("array_list5",coordinates);
How to use parcelable with List or ArrayList - What is
the syntax?
Do it as:
ArrayList<String> arrCoordinates = new ArrayList<>(coordinates.size());
arrCoordinates.addAll(coordinates);
b.putParcelableArrayList("array_list5",arrCoordinates);
and get array_list5 as from Bundle
ArrayList<LatLng> coordinates = b.getParcelableArrayList("array_list5");

How to pass Multiple information from one screen to another screen in android

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()?

Android - Store data in Array of Objects and display later in ListView

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.

Initialising array passed from another activity

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);

Categories

Resources