如何通过使用Parcelable界面在Android的意图,通过与绘制参数包含到另一个活动的对象 [英] how to pass an object containing with drawable parameter to another activity through intent using Parcelable Interface In android

查看:176
本文介绍了如何通过使用Parcelable界面在Android的意图,通过与绘制参数包含到另一个活动的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用含有Parcelable Inteface到另一个活动一个绘制场中的目标。但问题是,在 公共无效writeToParcel(包裹包裹,INT标志)覆盖类(其对象发送)的方法,我不能有parcel.writeDrawable一种方法。所以,其他领域我能顺利通过,而不是绘制对象。 我已经添加下面的方法在我的班级实施Parcelable界面后。

I want to pass an object containing a drawable field using Parcelable Inteface to another Activity. But problem is, in public void writeToParcel(Parcel parcel, int flags) overridden method in class(whose object has to send), I can't have "parcel.writeDrawable" kind of method. So other fields I'm able to pass, but not Drawable. I have added the following methods in my class after implementing Parcelable Interface.

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

    public ImageInfo createFromParcel(Parcel source) {
        return new ImageInfo(source);
    }

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

public int describeContents() {
    return 1;
}

public void writeToParcel(Parcel parcel, int flags) {
    parcel.writeString(name);   
    parcel.writeString(price);
    parcel.writeString(urlOfImage);
}

和使用以下code我把我的对象的意图

and using following code I'm putting my object in intent

Bundle b = new Bundle();
b.putParcelable("parse", imageInfo);
intentTrackIntHome.putExtras(b);

所以我必须做的传递绘制现场。

So what I have to do to pass that drawable field.

我知道我可以使用共享preferences,SD卡,全球应用程序类,静态字段;但我不希望使用它。因为在未来我code可以改变的,所以我想稳健code。

I know I can use shared Preferences, sd card, global application class, static fields; but I don't want to use it. Because in future my code can change, so I want robust code.

推荐答案

将您绘制对象为位图,并将其发送给另一个活动。

Convert your Drawable to Bitmap and send it to Another Activity.

Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
intent.putExtra("Bitmap", bitmap);

要获取,这一意图

Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");

有关更多详细信息请点击此链接

这篇关于如何通过使用Parcelable界面在Android的意图,通过与绘制参数包含到另一个活动的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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