Initialising array passed from another activity - android

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

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

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.

How to get results in new activity from the previous activity results in android?

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

pass array of values between two activities

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

Categories

Resources