2011-07-18 7 views
0

Intent.putExtra(String、String)やBundle.putString(String、String)など、これに対する多くの簡単な解決策が見つかりましたが、これは私の状況には役立ちません。アクティビティ間でカスタムクラスを渡す方法

私は、非プリミティブ型を含むMyMP3というクラスを持っています。私はMyMP3のために以下を渡す必要があります...

private AudioFile audioFile; 
private Tag tag; 
private int index; 
private boolean saved, startedWithLyrics; 
private String id3lyrics; 

AudioFileとTagはどちらも.jarファイルからインポートしたクラスです。これらをインテント経由で別のアクティビティに渡すにはどうすればいいですか?私は "MyMP3"クラスのためにParcelableを実装しようとしましたが、プリミティブ型を渡さないときにこれらのメソッドを正しく使う方法がわかりません。

私を助けて下のコードを見て、私のようなカスタムクラスでParcelableを正しく使う方法を教えてください。 writeToParcel関数でパーセルを設定するにはどうすればいいですか?別のアクティビティでクラスを正しく取得するにはどうすればよいですか?

以下は私のコードです(少なくとも重要な部分です)。私は数日の間、いろいろなことを試してきましたが、それを動作させることはできません。私を助けてください!

public class MyMP3 extends AudioFile implements Parcelable 
{ 
private AudioFile audioFile; 
private Tag tag; 
private int index; 
private boolean saved, startedWithLyrics; 
private String id3lyrics; 

public MyMP3(File f, int index) 
{ 
    this.audioFile = AudioFileIO.read(f); 
    this.tag = this.audioFile.getTag(); 
    this.index = index; 
    this.saved = false; 
    this.id3lyrics = getLyrics(); 
} 

@Override 
public int describeContents() 
{ 
    return 0; 
} 

@Override 
public void writeToParcel(Parcel out, int flats) 
{ 
    /* This method does not work, but I do not know how else to implement it */ 

    Object objects[] = {this.audioFile, this.tag, this.index, this.saved, this.startedWithLyrics, this.id3lyrics}; 
    out.writeArray(objects); 
} 

public static final Parcelable.Creator<MyMP3> CREATOR = new Parcelable.Creator<MyMP3>() 
{ 
    public MyMP3 createFromParcel(Parcel in) 
    { 
     /* Taken from the Android Developer website */ 
     return new MyMP3(in); 
    } 

    public MyMP3[] newArray(int size) 
    { 
     /* Taken from the Android Developer website */ 
     return new MyMP3[size]; 
    } 
}; 

private MyMP3(Parcel in) 
{ 
     /* This method probable needs changed as well */ 
    Object objects[] = in.readArray(MyMP3.class.getClassLoader()); 
} 

}

+0

AudioFile/Tagプリミティブのメンバーですか?もしそうなら、おそらく、あなたのプロジェクトでAudioFileとTagを拡張し、これらのオブジェクトに対してパーセル可能なものを実装してメインプロジェクトで使用することができます。 – Mandel

答えて

1

あなたはそのようなあなたのMyMP3クラスParcelableを作ることができます。読み書き順序が正しいことを確認してください。非プリミティブもParcelableでなければならないので、残念ながらそれを制御できないかもしれません。あるいは、独自のシリアライズ/デシリアライズを行うこともできます。 JSONやXMLのようなテキスト形式を使用できます。もう1つの選択肢は、サブクラスのアプリケーション(マニフェストで宣言してください)を使用し、アクティビティをまたがるオブジェクトをハングする場所として使用することです。これは、あなたのアプリのライフサイクルのためにオブジェクトをメモリに保持するので、これに注意してください。

関連する問題