简体   繁体   English

传递arraylists的arraylist

[英]Passing an arraylist of arraylists

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. 我知道这是一个常见问题,我在网上查看了解决方案,但是我很难自行实现。

I have a class which contains three variables: Location , Date and Uri . 我有一个包含三个变量的类: LocationDateUri

I have an Arraylist which consists of multiple Arraylists that contains said class. 我有一个Arraylist ,其中包含包含上述类的多个Arraylist。

For instance: 例如:

ArrayList<ArrayList<class>>

I am trying to pass this onto another activity, yet unsuccessful. 我试图将其传递给另一项活动,但未成功。 I tried both Parcelable and Serializable but none worked. 我同时尝试了ParcelableSerializable但均Parcelable

Edit: Source code added. 编辑:添加了源代码。

Class documentation: 类文档:

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

}

First activity: 第一次活动:

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

Second activity: 第二项活动:

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

I think that you can use Gson too. 我认为您也可以使用Gson。

In your build.gradle 在您的build.gradle中

implementation 'com.google.code.gson:gson:2.8.4'

In the first activity 在第一个活动中

Gson gson = new Gson();
String json = gson.toJson(yourObject);
intent.putExtra("yourKey", json);

In the second activity 第二个活动中

Gson gson = new Gson();
YourObject yourObject = gson.fromJson(getIntent().getStringExtra("yourKey"), YourObject.class);

Easy and fast. 方便快捷。

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. 我认为最好的做法是创建一个实现Parcelable的新类,并在活动之间传递该新类的实例。

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
}

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. 一旦你有了这个类,你可以在你的数据是如何瓜分的完全控制,而且让你做一些事情,你将无法与Android内置的产品做。

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

Ok. 好。

Your Model 您的模型

public class ImageHolder implements Serializable {

    // Use String here, to avoid serialization problems
    private String uri;
    private Date date;
    private Location loc;

    public ImageHolder() {
    }

    public ImageHolder(String uri, Date date, Location loc) {
        this.uri = uri;
        this.date = date;
        this.loc = loc;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Location getLoc() {
        return loc;
    }

    public void setLoc(Location loc) {
        this.loc = loc;
    }

    @Override
    public String toString() {
        return "ImageHolder{" +
                "uri=" + uri +
                ", date=" + date +
                ", loc=" + loc +
                '}';
    }
}

Create a Container class 创建一个容器

public class Container implements Serializable {

    private ArrayList<ImageHolder> list;

    public Container() {
    }

    public Container(ArrayList<ImageHolder> list) {
        this.list = list;
    }

    public ArrayList<ImageHolder> getList() {
        return list;
    }

    public void setList(ArrayList<ImageHolder> list) {
        this.list = list;
    }

    @Override
    public String toString() {
        return "Container{" +
                "list=" + list +
                '}';
    }
}

Your First Activity 您的第一个活动

public class MainActivity extends AppCompatActivity {

    public static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        ImageHolder imageHolder0 = new ImageHolder("http://www.stackoverflow.com", new Date(), new Location("test0"));
        ImageHolder imageHolder1 = new ImageHolder("http://www.google.com", new Date(), new Location("test1"));

        ArrayList<ImageHolder> list = new ArrayList<>();
        list.add(imageHolder0);
        list.add(imageHolder1);

        Container container = new Container(list);

        Gson gson = new Gson();
        String json = gson.toJson(container);

        Intent intent = new Intent(MainActivity.this, TestActivity.class);
        intent.putExtra("yourKey", json);

        startActivity(intent);
    }
}

Your second activity 您的第二项活动

public class TestActivity extends AppCompatActivity {
    public static final String TAG = TestActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        Gson gson = new Gson();
        Container container = gson.fromJson(getIntent().getStringExtra("yourKey"), Container.class);

        Log.d(TAG, "OK?" + container.getList().toString());

        // Here convert String uri Fields to Uri Objects
        // Example:
        Uri uri = Uri.parse(container.getList().get(0).getUri());
    }
}


Logcat result: 08-01 11:10:22.097 6671-6671/it.darksurfer.english.template D/TestActivity: OK?[ImageHolder{uri=http://www.stackoverflow.com, date=Wed Aug 01 11:10:21 GMT+02:00 2018, loc=Location[test0 0,000000,0,000000 acc=??? t=?!? et=?!?]}, ImageHolder{uri=http://www.google.com, date=Wed Aug 01 11:10:21 GMT+02:00 2018, loc=Location[test1 0,000000,0,000000 acc=??? t=?!? et=?!?]}] Logcat结果: 08-01 11:10:22.097 6671-6671/it.darksurfer.english.template D/TestActivity: OK?[ImageHolder{uri=http://www.stackoverflow.com, date=Wed Aug 01 11:10:21 GMT+02:00 2018, loc=Location[test0 0,000000,0,000000 acc=??? t=?!? et=?!?]}, ImageHolder{uri=http://www.google.com, date=Wed Aug 01 11:10:21 GMT+02:00 2018, loc=Location[test1 0,000000,0,000000 acc=??? t=?!? et=?!?]}] 08-01 11:10:22.097 6671-6671/it.darksurfer.english.template D/TestActivity: OK?[ImageHolder{uri=http://www.stackoverflow.com, date=Wed Aug 01 11:10:21 GMT+02:00 2018, loc=Location[test0 0,000000,0,000000 acc=??? t=?!? et=?!?]}, ImageHolder{uri=http://www.google.com, date=Wed Aug 01 11:10:21 GMT+02:00 2018, loc=Location[test1 0,000000,0,000000 acc=??? t=?!? et=?!?]}]



Obviously you can put an undefined number of others ArrayList in the list of Container class! 显然,您可以在容器类的列表中放置未定义数量的其他ArrayList!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM