Sending Arrays with Intent.Putextra

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



Leave a reply



Submit