gpt4 book ai didi

android - Fragments之间直接传递复杂数据?代码有什么问题?

转载 作者:行者123 更新时间:2023-11-30 03:25:05 25 4
gpt4 key购买 nike

我想在 2 个 fragment 之间传递数据。我传递的数据是 SongDetails 类的对象。这是传递数据的 fragment 的代码

ArrayList<SongDetails> Songinfo =new........;
if (Songinfo.size()>0)//Songinfo is an object of the class SongDetails..

{
Bundle bundle = new Bundle();

bundle.putParcelableArrayList("Fragdata",Songinfo);
dv.setArguments(bundle);

}

接收数据的代码

Bundle bundle=this.getArguments(); 
final ArrayList<SongDetails> Songinfo =bundle.getParcelableArrayList("Fragdata");

当我运行它时,应用程序崩溃了...我做错了什么??

歌曲详情的代码

package sourcecode.jazzplayer;


import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;

public class SongDetails implements Parcelable {
Bitmap icon;
String song;
String Artist;
String Album;
String Path;
int time;
int icLauncher;

public SongDetails() {
}

public SongDetails(Parcel in) {
String[] data = new String[4];
in.readStringArray(data);
this.Path = data[0];
this.song= data[1];
this.Album= data[2];
this.Artist = data[3];
}

public String getSong() {
return song;
}

public void setSong(String song) {
this.song = song;
}

public String getArtist() {
return Artist;
}

public void setArtist(String Artist) {
this.Artist = Artist;
}

public Bitmap getIcon() {
return icon;
}

public void setIcon(Bitmap bitmap) {
this.icon = bitmap;
}

public String getPath2() {
return Path;
}

public void setPath2(String Path) {
this.Path = Path;
}

public String getAlbum() {
return Album;
}

public void setAlbum(String Album) {
this.Album = Album;
}

public void setIcon(int icLauncher) {
this.icLauncher = icLauncher;
}










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

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[] { this.Path,this.song,this.Album,this.Artist });

}

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public SongDetails createFromParcel(Parcel in) {
return new SongDetails(in);
}

public SongDetails[] newArray(int size) {
return new SongDetails[size];
}
};
}

最佳答案

您不能在 2 个 fragment 之间直接传递数据。相反,您必须通过您之前必须创建的接口(interface)将数据从您的 fragment 1 传递到您的 Activity 。

然后,在您的 Activity 中实现的方法中,您应该检索对 fragment 2 的对象引用并调用您创建的公共(public)方法并完成这项工作。

Android官方文档中有很好的教程:

Android Fragments

关于android - Fragments之间直接传递复杂数据?代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18366633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com