传递arraylists的arraylist [英] Passing an arraylist of arraylists

查看:85
本文介绍了传递arraylists的arraylist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个常见问题,我在网上查看了解决方案,但是我很难自行实现.

I know it's a commonly asked question and I looked at the solutions online, but I am having a difficulty implementing this on my own.

我有一个包含三个变量的类:LocationDateUri.

I have a class which contains three variables: Location, Date and Uri.

我有一个Arraylist,它由多个包含所述类的Arraylist组成.

I have an Arraylist which consists of multiple Arraylists that contains said class.

例如:

ArrayList<ArrayList<class>>

我正在尝试将其传递给另一项活动,但未成功. 我同时尝试了ParcelableSerializable,但是都没有.

I am trying to pass this onto another activity, yet unsuccessful. I tried both Parcelable and Serializable but none worked.

已添加源代码.

班级文档:

public class imageHolder implements Parcelable
{
private Uri uri;
private Date date;
private Location loc;

public imageHolder(Uri uriAdd, Date dateAdded,Location imgLoc)
{
    this.uri = uriAdd;
    this.date = dateAdded;
    this.loc = imgLoc;
}


public static final Creator<imageHolder> CREATOR = new Creator<imageHolder>() {
    @Override
    public imageHolder createFromParcel(Parcel in) {
        return new imageHolder(in);
    }

    @Override
    public imageHolder[] newArray(int size) {
        return new imageHolder[size];
    }
};

public Uri getURI() { return this.uri; }

public Date getDate() {return this.date; }

public Location getLocation() {return this.loc; };

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

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeSerializable(date);
    parcel.writeParcelable(uri,i);
    parcel.writeParcelable(loc, i);
}

protected imageHolder(Parcel in) {
    date = (java.util.Date) in.readSerializable();
    uri = in.readParcelable(Uri.class.getClassLoader());
    loc = in.readParcelable(Location.class.getClassLoader());
}

}

第一个活动:

ArrayList<ArrayList<imageHolder>> sepImages = new ArrayList<ArrayList<imageHolder>>();
    sepImages = groupPics(images);


    Intent nextActivity = new Intent(loadImages.this, storiesScreen.class);
    nextActivity.putExtra("images",images);
    startActivity(nextActivity);
    finishActivity(0);

第二活动:

ArrayList<ArrayList<imageHolder>> sepImages = 
(ArrayList<ArrayList<imageHolder>>) getIntent().getParcelableExtra("images");
Log.d("stories","test");

推荐答案

我认为最好的办法是创建一个实现Parcelable的新类,并在活动之间传递该新类的实例.

I think the best thing to do here would be to create a new class that implements Parcelable and pass instances of this new class between activities.

public class ParcelableListOfLists implements Parcelable {

    private ArrayList<ArrayList<imageHolder>> listOfLists;

    public ParcelableListOfLists(ArrayList<ArrayList<imageHolder>> listOfLists) {
        this.listOfLists = listOfLists;
    }

    public ArrayList<ArrayList<imageHolder>> getListOfLists() {
        return listOfLists;
    }

    // parcelable implementation here
}

一旦有了此类,您就可以完全控制数据的打包方式,这使您可以执行某些操作,而这些操作是使用内置Android系统无法完成的,在产品中.

Once you have this class, you can be in full control of how your data is parceled, and that lets you do some things that you won't be able to do with the Android built-in offerings.

这是打包列表清单的一种方法:

Here's one way you could parcel a list of lists:

@Override
public void writeToParcel(Parcel dest, int flags) {
    if (listOfLists != null) {
        dest.writeInt(listOfLists.size());

        for (ArrayList<imageHolder> list : listOfLists) {
            dest.writeTypedList(list);
        }
    } else {
        dest.writeInt(-1);
    }
}

另一方面,您可以重新创建列表列表,如下所示:

And on the other side, you can recreate the list of lists like this:

public ParcelableListOfLists(Parcel in) {
    int size = in.readInt();

    if (size != -1) {
        this.listOfLists = new ArrayList<>(size);

        for (int i = 0; i < size; ++i) {
            ArrayList<imageHolder> list = in.createTypedArrayList(imageHolder.CREATOR);
            listOfLists.add(list);
        }
    } else {
        this.listOfLists = null;
    }
}

所有这些结合在一起,您可以像这样在活动之间传递列表清单:

With all of this together, you can pass your list of lists between activities like this:

Intent nextActivity = new Intent(loadImages.this, storiesScreen.class);
nextActivity.putExtra("images", new ParcelableListOfLists(images));
startActivity(nextActivity);

并在下一个活动中像这样检索它们:

and retrieve them in the next activity like this:

ParcelableListOfLists plol = getIntent().getParcelableExtra("images");
ArrayList<ArrayList<imageHolder>> images = plol.getListOfLists();

这篇关于传递arraylists的arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆