从 ArrayList<Long> 到 Intent 的合理方式

我目前正在编写一个应用,该应用通过 json 从服务器提取一个长线数组,然后将该列表从一个活动传递到另一个活动。基本骨架如下所示:

public void onResponse(Map result)
{
    ArrayList handles= (ArrayList)result.get("fileHandles");

    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
    intent.putExtra("handles", handles);
}

因此,第一个问题变得很明显,putExtra 的唯一方法是 、 、 和 。考虑到Long可能是可包裹的,我错了它不起作用(即使我使用)。接下来,我以为我会通过一个长[],但是我没有看到从长[]到接受的直截了当的转换。这是我最终得到的解决方案:putIntegerArrayListExtraputStringArrayListExtraputCharSequenceArrayListExtraputParcelableArrayListExtraArrayList<Long>ArrayList<Long>intent.putExtra

ArrayList handles= (ArrayList)result.get("fileHandles");
long [] handleArray = new long[handles.size()];
for (int i = 0; i < handles.size(); i++)
{
    handleArray[i] = Long.parseLong(handles.get(i).toString());
}

显然,这对我来说似乎有点荒谬,但我尝试过的所有其他转换似乎都出于这样或那样的原因而抱怨。在我走到这一步之前,我曾考虑过重新思考我的序列化,以便解决问题,但我发现从一个活动传递到另一个活动可能如此困难,这很奇怪。有没有我错过的更明显的解决方案?ArrayList<Long>


答案 1

您可以将其用作额外用途。 is 和 extends,它还实现了:SerializableArrayListSerializableLongNumberSerializable

// Put as Serializable
i.putExtra("handles", handles);

// Unfortunately you'll get an unsafe cast warning here, but it's safe to use
ArrayList<Long> handles = (ArrayList<Long>) i.getSerializableExtra("handles");    

答案 2

两个选项:使用或ParcelableSerializable

在:

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("handles", new FileHandles(handles));

外:

FileHandles handles = intent.getParcelableExtra("handles");

对象:

import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;
import java.util.List;

public class FileHandles implements Parcelable {

    private final List<Long> fileHandles;

    public FileHandles(List<Long> fileHandles) {
        this.fileHandles = fileHandles;
    }

    public FileHandles(Parcel in) {
        int size = in.readInt();
        long[] parcelFileHandles = new long[size];
        in.readLongArray(parcelFileHandles);
        this.fileHandles = toObjects(size, parcelFileHandles);
    }

    private List<Long> toObjects(int size, long[] parcelFileHandles) {
        List<Long> primitiveConv = new ArrayList<Long>();
        for (int i = 0; i < size; i++) {
            primitiveConv.add(parcelFileHandles[i]);
        }
        return primitiveConv;
    }

    public List<Long> asList() { // Prefer you didn't use this method & added domain login here, but stackoverflow can only teach so much..
        return fileHandles;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(fileHandles.size());
        dest.writeLongArray(toPrimitives(fileHandles));
    }

    private static long[] toPrimitives(List<Long> list) {
        return toPrimitives(list.toArray(new Long[list.size()]));
    }

    public static long[] toPrimitives(Long... objects) {
        long[] primitives = new long[objects.length];
        for (int i = 0; i < objects.length; i++)
            primitives[i] = objects[i];

        return primitives;
    }

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

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

可序列化(强制使用哪个实现( 不)ArrayListSerializableList

在:

    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
    intent.putExtra("handles", new ArrayList<Long>());

外:

    ArrayList handles = (ArrayList) intent.getSerializableExtra("handles");

推荐