将 ArrayList<Object> 传递回 main Activity (Android)
Passing ArrayList<Object> back to main Activity (Android)
我有一个 ArrayList< Object>,其中 Object 是我创建的 class 的一个实例。我想将这个新数组列表传递回主 activity 以将其放入导航抽屉项目中。
如何将 ArrayList
我试过 bundle.getParcelableArrayList 但是它告诉我 "Inferred type Object for type parameter T is not within its bounds, should implement android.os.parcelable"
这是我的代码:
public void createPlaylist(String playlistName, ArrayList<Song> newPlaylistSongsArray) {
Intent returnIntent = new Intent();
Bundle b = new Bundle();
b.putParcelableArrayList("array", (ArrayList<? extends Parcelable>) newPlaylistSongsArray);
returnIntent.putExtra("playListName", playlistName);
returnIntent.putExtra("array", newPlaylistSongsArray);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
并且:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK){
String newPlaylistName = data.getStringExtra("playListName");
Bundle b = getIntent().getExtras();
final ArrayList<Song> newPlaylist = b.getParcelableArrayList("array");
Toast.makeText(MP3Player.this, "New Playlist Created", Toast.LENGTH_LONG).show();
}
if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(MP3Player.this, "Shit went wrong yo!", Toast.LENGTH_LONG).show();
}
}
}
为了最简单的方法,您的歌曲 class 需要实现 Serializable。
Bundle bundle = new Bundle();
bundle.putSerializable("newPlaylist", newPlaylist);
intent.putExtras(bundle);
其他 activity:
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
ArrayList<Song> newPlaylist=
(ArrayList<Song>)bundle.getSerializable("newPlaylist");
我有一个 ArrayList< Object>,其中 Object 是我创建的 class 的一个实例。我想将这个新数组列表传递回主 activity 以将其放入导航抽屉项目中。
如何将 ArrayList
我试过 bundle.getParcelableArrayList 但是它告诉我 "Inferred type Object for type parameter T is not within its bounds, should implement android.os.parcelable"
这是我的代码:
public void createPlaylist(String playlistName, ArrayList<Song> newPlaylistSongsArray) {
Intent returnIntent = new Intent();
Bundle b = new Bundle();
b.putParcelableArrayList("array", (ArrayList<? extends Parcelable>) newPlaylistSongsArray);
returnIntent.putExtra("playListName", playlistName);
returnIntent.putExtra("array", newPlaylistSongsArray);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
并且:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK){
String newPlaylistName = data.getStringExtra("playListName");
Bundle b = getIntent().getExtras();
final ArrayList<Song> newPlaylist = b.getParcelableArrayList("array");
Toast.makeText(MP3Player.this, "New Playlist Created", Toast.LENGTH_LONG).show();
}
if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(MP3Player.this, "Shit went wrong yo!", Toast.LENGTH_LONG).show();
}
}
}
为了最简单的方法,您的歌曲 class 需要实现 Serializable。
Bundle bundle = new Bundle();
bundle.putSerializable("newPlaylist", newPlaylist);
intent.putExtras(bundle);
其他 activity:
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
ArrayList<Song> newPlaylist=
(ArrayList<Song>)bundle.getSerializable("newPlaylist");