在我试图制作parcelable的类中,有一个叫做genericSongClass的小类。在较大的类中,有一个genericSongClass的ArrayList。所以我知道我必须使genericSongClass可以使用,但是如果不使类静态,我就不能给它一个CREATOR。我有什么方法可以做到这一点吗?
我现在处于困境中的小班:
public class genericSongClass implements Parcelable {
String songTitle = "";
String songArtist = "";
String songData = "";
String songAlbum = "";
String isChecked = "false";
public final static Parcelable.Creator<genericSongClass> CREATOR = new Parcelable.Creator<genericSongClass>() {
@Override
public genericSongClass createFromParcel(Parcel source) {
return null;
}
@Override
public genericSongClass[] newArray(int size) {
return null;
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
}
My Larger class(所有parcelable相关方法位于底部,使用parcel的ArrayList和构造函数位于顶部):
public class Music implements Parcelable{
private static ArrayList<genericSongClass> songs = null;
Cursor cursor;
Context context;
public Music(Context context){
this.context = context;
}
public Music(Parcel in){
songs = in.readArrayList(songs, genericSongClass.CREATOR);
}
public void BindAllSongs() {
/** Making custom drawable */
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
final String[] projection = new String[] {
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM};
final String sortOrder = MediaStore.Audio.AudioColumns.TITLE
+ " COLLATE LOCALIZED ASC";
try {
// the uri of the table that we want to query
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
// query the db
cursor = context.getContentResolver().query(uri,
projection, selection, null, sortOrder);
if (cursor != null) {
songs = new ArrayList<genericSongClass>(cursor.getCount());
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
genericSongClass GSC = new genericSongClass();
GSC.songTitle = cursor.getString(0);
GSC.songArtist = cursor.getString(1);
GSC.songData = cursor.getString(2);
GSC.songAlbum = cursor.getString(3);
songs.add(GSC);
cursor.moveToNext();
}
}
} catch (Exception ex) {
} finally {
if (cursor != null) {
cursor.close();
}
}
}
public static Object[] toArray(ArrayList<Object> list){
Object[] toReturn = new Object[list.size()];
for (int i = 0; i < list.size(); i++){
toReturn[i] = list.get(i);
}
return toReturn;
}
public ArrayList<String> getArtists(){
ArrayList<String> artists = new ArrayList<String>();
for(genericSongClass gsc: songs){
if(!artists.contains(gsc.songArtist)){
artists.add(gsc.songArtist);
}
}
Alphabetize forArtists = new Alphabetize(artists);
return forArtists.getSortedArrayList();
}
public ArrayList<String> getAlbums(String artist){
ArrayList<String> albums = new ArrayList<String>();
for(genericSongClass gsc: songs){
if(gsc.songArtist == artist){
albums.add(gsc.songAlbum);
}
}
Alphabetize forAlbums = new Alphabetize(albums);
return forAlbums.getSortedArrayList();
}
//--- Parcel ------------------------------------------------
public static final Parcelable.Creator<Music> CREATOR = new Parcelable.Creator<Music>() {
public Music createFromParcel(Parcel in) {
return new Music(in);
}
@Override
public Music[] newArray(int size) {
return null;
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(songs);
}
}
这是我第一次尝试使用Parcelable,欢迎任何帮助!提前谢谢!
答案 0 :(得分:3)
首先关闭:考虑遵循Java惯例并将genericSongClass
重命名为GenericSong
。其余部分假设您没有更改该名称。
第二关:在使用/了解Parcelables时阅读这本惊人的指南 http://blog.logicexception.com/2012/09/a-parcelable-tutorial-for-android.html
最后:您确切代码的部分或全部解决方案:
在genericSongClass
中添加新的构造函数:
public genericSongClass(Parcel in) {
songTitle = in.readString();
// read in the rest of your values here
}
并添加默认构造函数:
public genericSongClass() {
}
CREATOR
中的genericSongClass
应如下所示:(同时调整Music
类
public final static Parcelable.Creator<genericSongClass> CREATOR = new Parcelable.Creator<genericSongClass>() {
@Override
public genericSongClass createFromParcel(Parcel source) {
return new genericSongClass(source);
}
@Override
public genericSongClass[] newArray(int size) {
return new genericSongClass[size];
}
};
调整writeToParcel
方法:
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(songTitle);
// write your other values here
}
保持秩序,这是重要的!读取和写入的顺序应该相同!
在Music
类构造函数中:
in.readTypedList(songs, genericSongClass.CREATOR);
在Music
班writeToParcel
:
dest.writeTypedList(songs);