如何将自定义对象变量转换为新活动?

时间:2017-12-16 02:27:07

标签: java android

好的,所以我希望根据在主活动中初始化的对象拉伸来设置每个活动的图像视图。我无法将其设置为静态因为getStringgetResources,并且无法在静态上下文中使用它。

以下是主要活动的相关部分:

public class MainActivity extends AppCompatActivity {
    public static int pos = 0, time = 60;
    public static Bundle bundle = new Bundle();

    final stretch[] stretches = new stretch[]{new stretch(this, 
            ResourcesCompat.getDrawable(getResources(), R.drawable.pic1, null),
                    true, getString(R.string.SEdescription)),
                    new stretch(this, ResourcesCompat.getDrawable(getResources(), 
            R.drawable.pic2, null),
                            true, getString(R.string.USSdescription)),
                    new stretch(this, ResourcesCompat.getDrawable(getResources(), 
            R.drawable.pic2, null),
                            true, getString(R.string.RHCdescription)),
                    new stretch(this, ResourcesCompat.getDrawable(getResources(), 
            R.drawable.pic2, null),
                            true, getString(R.string.FSdescription)),
                    new stretch(this, ResourcesCompat.getDrawable(getResources(), 
            R.drawable.pic2, null),
                            true, getString(R.string.SPdescription)),
                    new stretch(this, ResourcesCompat.getDrawable(getResources(), 
            R.drawable.pic2, null),
                            true, getString(R.string.KLdescription)),
                    new stretch(this, ResourcesCompat.getDrawable(getResources(), 
            R.drawable.pic2, null),
                            true, getString(R.string.BFdescription)),
                    new stretch(this, ResourcesCompat.getDrawable(getResources(), 
            R.drawable.pic2, null),
                            true, getString(R.string.BBdescription)),
                    new stretch(this, ResourcesCompat.getDrawable(getResources(), 
            R.drawable.pic2, null),
                            true, getString(R.string.LTdescription))
    };

如何在其他活动中访问此数组,以便我可以执行stretch[1].getImage();之类的操作?

2 个答案:

答案 0 :(得分:1)

首先,您需要为自定义对象创建一个类,并且我已经从您的代码中创建了一些类。

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;

public class stretch implements Parcelable {

    private Context myContext;
    private Drawable myDrawable;
    private boolean myBoolean;
    private String myString;

    public final static Parcelable.Creator<stretch> CREATOR = new Creator<stretch>() {
        @SuppressWarnings({ "unchecked" })
        public stretch createFromParcel(Parcel in) {
            return new stretch(in);
        }

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

    protected stretch(Parcel in) {
        this.myContext = ((Context) in.readValue((Context.class.getClassLoader())));
        this.myDrawable = ((Drawable) in.readValue((Drawable.class.getClassLoader())));
        this.myBoolean = ((boolean) in.readValue((boolean.class.getClassLoader())));
        this.myString = ((String) in.readValue((String.class.getClassLoader())));
    }
    public stretch(Context myContext, Drawable myDrawable, boolean myBoolean, String object) {
        this.myContext = myContext;
        this.myDrawable = myDrawable;
        this.myBoolean = myBoolean;
        this.myString = object;
    }

    public Context getMyContext() {
        return myContext;
    }

    public void setMyContext(Context myContext) {
        this.myContext = myContext;
    }

    public Drawable getMyDrawable() {
        return myDrawable;
    }

    public void setMyDrawable(Drawable myDrawable) {
        this.myDrawable = myDrawable;
    }

    public boolean isMyBoolean() {
        return myBoolean;
    }

    public void setMyBoolean(boolean myBoolean) {
        this.myBoolean = myBoolean;
    }

    public String getMyString() {
        return myString;
    }

    public void setMyString(String myString) {
        this.myString = myString;
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeValue(myContext);
        dest.writeValue(myDrawable);
        dest.writeValue(myBoolean);
        dest.writeValue(myString);
    }

    public int describeContents() {
        return 0;
    }
}

在您的活动更改中,

  

final stretch []延伸

ArrayList<stretch> stretches = new ArrayList(); 

然后使用

添加项目
stretches.add(new stretch(this, ResourcesCompat.getDrawable(getResources(), R.drawable.pic1, null), true, getString(R.string.SEdescription)))
stretches.add(new stretch(this, ResourcesCompat.getDrawable(getResources(), R.drawable.pic2, null), true, getString(R.string.USSdescription)))
//likewise add all other items

并将此传递给活动,

startActivity(new Intent(context, NextActivity.class)
        .putParcelableArrayListExtra("stretches", stretches));

要在NextActivity

中获取数组
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ArrayList<stretch> stretchList = getIntent().getParcelableArrayListExtra("stretches");
}

答案 1 :(得分:0)

您可以尝试一些方法

<强> 1。创建课程

public class SomeClassYouWant implements Parcelable {
@DrawableRes
int drawableIconOrImage;
Object idkWhyYourPassNull;
@StringRes
int stringYouWant;

protected SomeClassYouWant(Parcel in) {
    drawableIconOrImage = in.readInt();
    stringYouWant = in.readInt();
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(drawableIconOrImage);
    dest.writeInt(stringYouWant);
}

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

public static final Creator<SomeClassYouWant> CREATOR = new Creator<SomeClassYouWant>() {
    @Override
    public SomeClassYouWant createFromParcel(Parcel in) {
        return new SomeClassYouWant(in);
    }

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

}

List<SomeClassYouWant> mList= new ArrayList<>();

然后通过活动

startActivity(new Intent(context, SomeActivity.class)
            .putParcelableArrayListExtra("list", mList));

<强> 2。创建全局

Application类中创建变量并使用GettersSetters进行访问,只要您的应用程序正在运行,您的对象就会停留,但不推荐使用此方法