나는 행운이없는 간단한 예를 찾고있다.안드로이드를 부모 활동으로 되 돌리는 안드로이드
내 안드로이드 응용 프로그램에는 두 가지 활동이 있습니다. 1. 시작시 실행되는 기본 활동 2. 기본 활동의 버튼을 눌러 실행되는 두 번째 활동.
두 번째 활동이 끝나면 (버튼을 눌러서) MyObject 유형의 ArrayList를 주 활동에 보내고 닫으면 주 활동이 그걸로 무엇이든 할 수 있습니다. 이 일을 어떻게 성취 할 수 있습니까? 나는 몇 가지 시도를 해왔지만 두 번째 활동을 시작할 때 내 응용 프로그램이 충돌합니다.
사용자가 제 활동을 시작 버튼을 누를
: Intent i = new Intent(MainActivity.this, secondactivity.class);
startActivityForResult(i, 1);
제 활성의 버튼을 누른 후에 다시 번들 배열 : 메인 마침내
Intent intent= getIntent();
Bundle b = new Bundle();
b.putParcelableArrayList("myarraylist", mylist);
intent.putExtras(b);
setResult(RESULT_OK, intent);
finish();
그리고 청취자 활동 (이 코드가 시작될 때 나는 100 % 확신 할 수 없지만 ...)
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK && requestCode==1){
Bundle extras = data.getExtras();
final ArrayList<MyObject> mylist = extras.getParcelableArrayList("myarraylist");
Toast.makeText(MainActivity.this, mylist.get(0).getName(), Toast.LENGTH_SHORT).show();
}
}
내가 잘못 가고있는 어떤 생각? onActivityResult() 내 응용 프로그램을 충돌하는 것 같습니다.
편집 : parcelable 방법과 클래스 : 토스트, 그 빈 내부이라고 onActivityResult를
import android.os.Parcel;
import android.os.Parcelable;
public class Plan implements Parcelable{
private String name;
private String id;
public Plan(){
}
public Plan createFromParcel(Parcel in) {
Plan plan = new Plan();
plan.setId(in.readString());
plan.setName(in.readString());
return plan;
}
public Plan(String name, String id){
this.name = name;
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getId(){
return id;
}
public void setId(String id){
this.id = id;
}
public String toString(){
return "Plan ID: " + id + " Plan Name: " + name;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(name);
}
public static final Parcelable.Creator<Plan> CREATOR
= new Parcelable.Creator<Plan>() {
public Plan createFromParcel(Parcel in) {
return new Plan();
}
@Override
public Plan[] newArray(int size) {
// TODO Auto-generated method stub
return new Plan[size];
}
};
}
두 번째 활동이 끝난 후,하지만 아무것도 표시됩니다. 어떤 아이디어? 나는 내 수업은 여전히
편집 ... 엉망 같은데요 : 그것은 내가 베드로가 잘못된 위치에 공급하는 방법을했다
을 일을 얻었다. 다음과 같이 내부 작성자 여야합니다.
public static final Parcelable.Creator<Plan> CREATOR
= new Parcelable.Creator<Plan>() {
public Plan createFromParcel(Parcel in) {
Plan plan = new Plan();
plan.setId(in.readString());
plan.setName(in.readString());
return plan;
}
아니요.
피터에게 많은 감사를드립니다. 희망이 다른 사람을 도와줍니다.
Logcat 출력을 제공 할 수 있습니까? 또한, * 두 번째 활동을 시작하면 앱이 충돌한다고합니다. 또 다른 문제가있을 수 있습니까? – Shade
메인 포스트에 추가 –