本文介绍了直接传递片段之间复杂的数据?什么是错的code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在2 fragments.the数据我传递传递数据是一类SongDetails.Here的对象是code为其传递数据片段

 的ArrayList< SongDetails> Songinfo =新........;
如果(Songinfo.size()&0)// Songinfo是类SongDetails的一个目的..        {
         束束=新包();         bundle.putParcelableArrayList(Fragdata,Songinfo);
         dv.setArguments(包);        }

code,用以接收数据

 捆绑包= this.getArguments();
    最终的ArrayList< SongDetails> Songinfo = bundle.getParcelableArrayList(Fragdata);

当我运行这一点,应用程序崩溃...我是什么做错了?

在code为SongDetails

 包源$ C ​​$ c.jazzplayer;
进口android.graphics.Bitmap;
进口android.os.Parcel;
进口android.os.Parcelable;公共类SongDetails实现Parcelable {
    位图图标;
    弦之歌;
    串艺术家;
    STRING相册;
    路径字符串;
    INT时间;
    INT icLauncher;    公共SongDetails(){
    }    公共SongDetails(包裹中){
        的String []数据=新的String [4];
        in.readStringArray(数据);
        this.Path =数据[0];
        this.song =数据[1];
        this.Album =数据[2];
        this.Artist =数据[3];
    }    公共字符串getSong(){
        返回歌曲;
    }    公共无效setSong(弦之歌){
        this.song =歌曲;
    }    公共字符串getArtist(){
        返回艺术家;
    }    公共无效setArtist(字符串艺术家){
        this.Artist =艺术家;
    }    公共位图调用getIcon(){
        返回图标;
    }    公共无效的setIcon(位图位图){
        this.icon =位图;
    }    公共字符串getPath2(){
        返回路径;
    }    公共无效setPath2(字符串路径){
        this.Path =路径;
    }    公共字符串getAlbum(){
        返回相册;
    }    公共无效setAlbum(字符串专辑){
        this.Album =专辑;
    }    公共无效的setIcon(INT icLauncher){
        this.icLauncher = icLauncher;
    }
    @覆盖
    公众诠释describeContents(){
        返回0;
    }    @覆盖
    公共无效writeToParcel(DEST包裹,INT标志){
        dest.writeStringArray(新的String [] {this.Path,this.song,this.Album,this.Artist});    }    公共静态最终Parcelable.Creator CREATOR =新Parcelable.Creator(){
        公共SongDetails createFromParcel(包裹中){
            返回新SongDetails(中);
        }        公共SongDetails [] newArray(INT大小){
            返回新SongDetails【尺寸】;
        }
    };
}


解决方案

您不能直接2片段之间传递数据。相反,你必须通过你必须有previously创建的接口从片段1数据传递到您的活动。

然后,在你的活动中实现的方法,您应能检索对象引用片段2并调用您创建一个公共方法,做这项工作。

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

i want to pass data between 2 fragments.the data i am passing is an object of a class SongDetails.Here is the Code for the fragment which is passing the data

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);

        }

code for receiving the data

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

when i run this it the app crashes...what am i be doing wrong??

the code for the SongDetails

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];
        }
    };
}
解决方案

You can't pass data directly between 2 fragments. Instead you have to pass data from your fragment 1 to your activity through an interface that you must have previously created.

Then, inside the implemented method in your activity, you should retrieve the object reference to fragment 2 and call a public method that you created and do the job.

There is a good tutorial in Android official documentation:

Android Fragments

这篇关于直接传递片段之间复杂的数据?什么是错的code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:04