使用parcelable将对象发送到另一个活动

时间:2015-01-04 00:45:58

标签: android android-intent

我正在尝试将此类的对象发送到另一个活动。我使用parcelabler.com生成代码。问题是当我声明意图时,Eclispse说方法putExtra(String,GrupoMuscular)未定义类型Intent。我有错误的parcelable实现吗?

public class GrupoMuscular implements Parcelable {

    private int id;
    private static int id_aux = 0;
    private String nome;
    private int imagem;
    private ArrayList<Exercicio> exercicios;

    public GrupoMuscular(String nome, int img) {
        //super();
        id = id_aux;
        id_aux++;
        this.nome = nome;
        this.imagem = img;
        this.exercicios = new ArrayList<Exercicio>();
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public int getImagem() {
        return imagem;
    }

    public void setImagem(int imagem) {
        this.imagem = imagem;
    }

    public ArrayList<Exercicio> getGruposMuscular() {
        return exercicios;
    }

    public void addExercicio(int id, String nome, String descricao, int imagem){        
        Exercicio ex = new Exercicio(id, nome, descricao, imagem);
        exercicios.add(ex);
    }

    public void setGruposMuscular(ArrayList<Exercicio> gruposMuscular) {
        this.exercicios = gruposMuscular;
    }

    public ArrayList<Exercicio> getExercicios() {
        return exercicios;
    }

    public void setExercicios(ArrayList<Exercicio> exercicios) {
        this.exercicios = exercicios;
    }

    public int getId() {
        return id;

    protected GrupoMuscular(Parcel in) {
        id = in.readInt();
        nome = in.readString();
        imagem = in.readInt();
        if (in.readByte() == 0x01) {
            exercicios = new ArrayList<Exercicio>();
            in.readList(exercicios, Exercicio.class.getClassLoader());
        } else {
            exercicios = null;
        }
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(nome);
        dest.writeInt(imagem);
        if (exercicios == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeList(exercicios);
        }
    }

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

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

活动意图:

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent;

                intent = new Intent(ExerciciosActivity.this, ListaExerciciosActivity.class);
                intent.putExtra("musculos", gruposMusculares.get(position));
                startActivity(intent);
        }});
    }

1 个答案:

答案 0 :(得分:2)

尝试在Intent上将对象作为Parcelable Extra传递。

http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String,android.os.Parcelable)

因为您只传递了此Object的单个实例。

// Create a new Intent
Intent myNewIntent = new Intent(this, NextActivity.class);
myNewIntent.putExtra("musculos", groupMusculares.get(position));
startActivity(myNewIntent);

然后在你的NextActivity.class中: 使用

http://developer.android.com/reference/android/content/Intent.html#getParcelableExtra(java.lang.String)

 // Class Member variable
 GrupoMuscular mMuscleGroup;

 // inside onCreate
 Bundle extras = getIntent().getExtras();

 // If the extras are not null
 if(extras != null){
    mMuscleGroup = extras.getParcelableExtra("musculos");

     // if its not null do something with it
     if(mMuscleGroup != null){
         // Perform some logic 
         // ...
     }
 }