2016-10-18 4 views
0

ArrayList가 있습니다. < Object> 여기서 Object는 내가 만든 클래스의 인스턴스입니다. 이 새로운 배열 목록을 기본 활동으로 다시 전달하여 navigationdrawer 항목에 추가하려고합니다.ArrayList 전달 <Object> 주 활동으로 돌아 가기 (Android)

개체를 ArrayList <에 전달하면 어떻게 되나요?

나는 여기

내 코드입니다 "android.os.parcelable 구현해야 입력 매개 변수 T에 대한 유추 Object 형식이 아닌 그 경계 내에"는 bundle.getParcelableArrayList 그러나 그것은 하더군요 시도 :

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(); 
     } 
    } 
} 
+3

귀하의'Object' 클래스는'Parcelable'를 구현해야합니다. 먼저 사용자 지정 개체와 같은 복잡한 데이터를 인 텐트를 통해 직렬화하지 않고 보낼 수는 없습니다. – b85411

+0

'ArrayList '은'ArrayList '이 아닙니다. –

+0

싱글 톤 클래스를 선언하고이를 클래스에 추가 할 수 있습니다. 그리고 당신은 어떤 활동에서 이걸 액세스 할 수 있습니다 –

답변

1

, 당신의 노래 클래스는 Serializable를 구현 필요가있다.

Bundle bundle = new Bundle(); 
bundle.putSerializable("newPlaylist", newPlaylist); 
intent.putExtras(bundle); 
다른 활동

:

Intent intent = this.getIntent(); 
Bundle bundle = intent.getExtras(); 

ArrayList<Song> newPlaylist= 
       (ArrayList<Song>)bundle.getSerializable("newPlaylist"); 
+0

시리얼 클래스를 구현하기 위해 노래 클래스를 설정하지 않았습니다. – Danny

관련 문제