在我尝试使可包裹的类中,有一个较小的类称为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) {
}
}
我的Larger类(所有与parcelable相关的方法都在底部,而使用包裹的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,欢迎任何帮助!提前致谢!
最佳答案
首先,请考虑以下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);