传递数组列表的数组列表

编程入门 行业动态 更新时间:2024-10-28 12:26:41
本文介绍了传递数组列表的数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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,它由包含所述类的多个 Arraylists 组成.

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

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

更多推荐

[db:关键词]

本文发布于:2023-04-16 07:20:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/881982.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!