2011-03-05 2 views
1

나는 행운이없는 간단한 예를 찾고있다.안드로이드를 부모 활동으로 되 돌리는 안드로이드

내 안드로이드 응용 프로그램에는 두 가지 활동이 있습니다. 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; 
} 

아니요.

피터에게 많은 감사를드립니다. 희망이 다른 사람을 도와줍니다.

+0

Logcat 출력을 제공 할 수 있습니까? 또한, * 두 번째 활동을 시작하면 앱이 충돌한다고합니다. 또 다른 문제가있을 수 있습니까? – Shade

+0

메인 포스트에 추가 –

답변

2

MyObjectParcelable을 구현해야 Android에서 직렬로 묶을 때 직렬화/역 직렬화 할 수 있습니다.

는 업데이트 :

방법 이름은 createFromParcel입니다. 그리고 실제로는 소포의 데이터로 계획을 작성해야합니다.

public Plan createFromParcel(Parcel in) { 
     Plan plan = new Plan(); 
     plan.setId(in.readString()); 
     plan.setName(in.readString()); 
     return plan; 
} 
+0

그것은 parcelable을 구현합니다.내가 얻을 수있는 클래스의 하단이 비어있는 방법을 넣어 가지고 있지만 그것은 작동합니다 : \t @Override \t 공공 INT describeContents을() { \t \t // TODO 방법 스텁을 자동 생성 \t \t 반환 0; 당신이 실제로 뭔가를 구현해야한다 : \t} \t @Override \t 공공 무효 writeToParcel (소포의 최종 도착이 플래그를 INT) { 는 \t // TODO 자동 생성 방법은 문제가 그 \t \t \t} –

+0

스텁 \t 그 방법들에서. 시스템이 MyObject의 직렬화 된 형식을 작성해야 할 때 호출됩니다. –

+0

내부에서 구현해야 할 것은 무엇입니까? –

관련 문제