如何通过开放的我们的活动与使用捆绑数组 [英] How to pass an array of Uri between Activity using Bundle

查看:104
本文介绍了如何通过开放的我们的活动与使用捆绑数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要开放的阵列传递到另一个活动, 通过字符串数组我用简单的

I need to pass an array of Uri to another activity, to pass an array of String I use simply

String[] images=getImagesPathString();

    Bundle b = new Bundle();
    b.putStringArray("images", images);

但是用开放的阵列

But using an array of Uri

 Uri[] imagesUri=getImagesUri();

这不工作,因为没有在捆绑的方法putUri(URI X)

this doesn't works because there isn't a method "putUri(Uri x)" in Bundle

我怎么能解决这个问题?

How could I solve this problem?

推荐答案

您应该考虑的Parcelable界面,看看如何通过东西意向

You should look into the Parcelable interface to see how to pass things on an intent

http://developer.android.com/intl/ ES /参考/安卓/ OS / Parcelable.html

也许你可以实现实现该接口ParcelableUri类。

Maybe you can implement a ParcelableUri class that implements that interface.

这样的(未测试!!):

Like this (not tested!!):

public class ParcelableUri implements Parcelable {

private Uri[] uris;

public ParcelableUri(Parcel in) {
    Uri.Builder builder = new Uri.Builder();

    int lenght = in.readInt();
    for(int i=0; i<=lenght; i++){           
        uris[i]= builder.path(in.readString()).build();
    }
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(uris.length);
    for(int i=0; i<=uris.length; i++){
        dest.writeString(uris[i].toString());
    }
}

public static final Parcelable.Creator<ParcelableUri> CREATOR = new Parcelable.Creator<ParcelableUri>() {

    public ParcelableUri createFromParcel(Parcel in) {
        return new ParcelableUri(in);
    }

    public ParcelableUri[] newArray(int size) {
        return new ParcelableUri[size];
    }
};

这篇关于如何通过开放的我们的活动与使用捆绑数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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