Sending arrays with Intent.putExtra
You are setting the extra with an array. You are then trying to get a single int.
Your code should be:
int[] arrayB = extras.getIntArray("numbers");
Passing array of Parcelable using Intent.putExtra
There is no way to directly convert the array as you are trying to do.
The return value of getParcelableArrayExtra("COMPLETED_SONGS")
as far as android's OS is concerned is an array of Parcelable
that just happens to contain just Song
instances.
To use it directly you can use the Array.filterIsInstance
function to create List<Song>
that is guaranteed to be type-safe. If it absolutely needs to be an array then .toTypedArray()
will convert it back to Array<Song>
This should result in the following code: val songs = getParcelableArrayExtra("COMPLETED_SONGS").filterIsInstance<Song>().toTypedArray()
how can i send the string value into the intent.putextra?
use this line to get selected item from spinner
String str=spinner.getSelectedItem().toString();
Unable to pass array of objects via intent extras
That is the expected behaviour because when you modify your array list at DetailActivity
you are working with a different instance of your movies list.
You can get the changes you make to the list by starting DetailActivity
with startActivityForResult
and then sending back the updated list to your MainActivity
You would have something like this at your MainActivity
ArrayList<String> mMovies;
int DETAIL_REQUEST_CODE = 123;
String KEY_FAVORITE_MOVIES = "key-movies";
private void startDetailActivity() {
Intent intent = new Intent(this, DetailActivity.class);
intent.putStringArrayListExtra(KEY_FAVORITE_MOVIES, mMovies);
startActivityForResult(intent, DETAIL_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == DETAIL_REQUEST_CODE && resultCode == RESULT_OK) {
mMovies = data.getStringArrayListExtra(KEY_FAVORITE_MOVIES);
}
}
And you will have to send the updated list from the DetailActivity
with something like this:
public void finishDetail() {
Intent resultIntent = new Intent();
resultIntent.putStringArrayListExtra(KEY_FAVORITE_MOVIES, mMovies);
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
Passing an array with android-intent
you should use:
class A:
int array[] = {1,2,3};
Intent i = new Intent(A.this, B.class);
i.putExtra("numbers", array);
startActivity(i);
class B:
Bundle extras = getIntent().getExtras();
int[] arrayB = extras.getIntArray("numbers");
Related Topics
Maximum Java Heap Size of a 32-Bit Jvm on a 64-Bit Os
Firestore - Merging Two Queries Locally
Using Singleton Design Pattern for SQLitedatabase
How to Set Java_Home Path on Ubuntu
How to Open a Unc Path from Linux in Java
Force Jvm to Do All Io Without Page Cache (E.G. O_Direct)
How to Include Platform-Specific Native Libraries in the .Jar File Using Eclipse
Synchronized Block on Grails Works on Windows But No in Linux
Match Multiline Text Using Regular Expression
How to Avoid Installing "Unlimited Strength" Jce Policy Files When Deploying an Application
Android Jdbc Not Working: Classnotfoundexception on Driver
Image Encryption/Decryption Using Aes256 Symmetric Block Ciphers
What's the Location of the Javafx Runtime Jar File, Jfxrt.Jar, on Linux
How to Use Chef to Update-Alternatives for Java Using Execute