안드로이드 Serializable 질문드립니다.
조회수 1775회
Serializable Intent 질문드립니다.
ArrayList의 data class를 Serializable implements 하고 단순히 putExtra를 보내주면 받는쪽에서 getSerializableExtra 하면 되는건가요~? 그리고 데이터가 잘 도착했는지 확인할 수 있는 방법 좀 알려주시면 감사하겠습니다.
public class ItemData implements Serializable {
String title;
String maintext;
public SingerItem(String title, String maintext) {
this.title = title;
this.maintext = maintext;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMaintext() {
return maintext;
}
public void setMaintext(String maintext) {
this.maintext = maintext;
}
}
보내는쪽
Intent intent = new Intent(getApplicationContext(), secondActivity.class);
ArrayList<ItemData > items = new ArrayList<ItemData >();
intent.putExtra("items", items);
startActivity(intent);
받는쪽
Intent intent = getIntent();
ArrayList<ItemData > dataset = (ArrayList<ItemData >) intent.getSerializableExtra("items");
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
네 위 방법대로 하시면 될 것 같습니다. 만약 intent 로 부터 정상적으로 전달되지 않을 경우 null 을 return 할 것이므로 null check 부분을 추가 해주시면 될 것 같네요.
Intent intent = getIntent(); ArrayList<ItemData > dataset = (ArrayList<ItemData >) intent.getSerializableExtra("items"); if(dataset != null){ //do something }
-
(•́ ✖ •̀)
알 수 없는 사용자
-
댓글 입력