簡體   English   中英

在另一個活動中傳遞對象的數組列表

[英]Pass Arraylist of objects in another activity

我正在嘗試在另一個活動中發送對象的數組列表。 我已經檢查了很多關於該主題的文章,並且我使用的是包裹式包裹,但是我無法發送對象。我嘗試了很多事情。這就是我正在嘗試的事情。

public class ParcalableForm implements Parcelable {

private ArrayList<form> from;


public ParcalableForm (ArrayList<form> choices) {
    this.from = choices;
}

public ParcalableForm (Parcel parcel) {
    this.from = parcel.readArrayList(null);
}



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

// Required method to write to Parcel
@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeList(from);
}

// Method to recreate a Question from a Parcel
public static Creator<ParcalableForm> CREATOR = new Creator<ParcalableForm>() {

    @Override
    public ParcalableForm createFromParcel(Parcel source) {
        return new ParcalableForm(source);
    }

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

};

}

這是實現Parcelable的parcelable類。我正在嘗試將表單的Arraylist發送到另一個活動。

  Intent i = new Intent(UserPage.this,Form.class);

  Bundle extras = new Bundle();
  System.out.println("I found a form :- ");
  ParcalableForm p=new ParcalableForm(f1.attr);
  i.putExtra("geopoints", p);
  startActivity(i);

這是將對象發送到其他活動的類。

Bundle extras = getIntent().getExtras();
ParcalableForm po =  new ParcalableForm(extras.getParcelableArrayList("geopoints"));

這是我不知道如何獲取對象/ Arraylist的部分。我嘗試了很多方法,但是沒有運氣。有什么想法嗎?

將數據傳遞到花葯活動

 ArrayList<Animal> animals = new ArrayList<Animal>();
//fill your list with animals here

i.putExtra("animals", animals);

接收數據

ArrayList<Animal> animals = (ArrayList<Animal>) getIntent()
                        .getSerializableExtra("animals");

如果要傳遞ArrayList<Form> ,則使用getParcelableArrayListExtra
通常,請按照下列步驟操作:

  • 使您的Form類正確實現Parcelable
  • 在活動中發送意圖:

    // ArrayList<Form> myList - data to send; intent.putParcelableArrayListExtra("geopoints", myList);

  • 在接收活動中:

    ArrayList<Form> myReceivedList = getIntent().getParcelableArrayListExtra("geopoints");

並且不要忘記null /健全性檢查。

希望能有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM