将对象列表从一个活动传递到android中的其他活动

Sus*_*gar 29 android android-intent

我想传递来自另一个活动的一个活动的对象列表.我在下面有一个类SharedBooking:

public class SharedBooking {
  public int account_id;
  public Double betrag;
  public Double betrag_effected;
  public int taxType;
  public int tax;
  public String postingText;
}
Run Code Online (Sandbox Code Playgroud)

来自呼叫活动的代码:

public List<SharedBooking> SharedBookingList = new ArrayList<SharedBooking>();

public void goDivision(Context context, Double betrag, List<SharedBooking> bookingList) {
  final Intent intent = new Intent(context, Division.class);    
  intent.putExtra(Constants.BETRAG, betrag);        
  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
  context.startActivity(intent);        
}
Run Code Online (Sandbox Code Playgroud)

被叫活动的代码:

Bundle extras = getIntent().getExtras();
if (extras != null) {
  amount = extras.getDouble(Constants.BETRAG,0);
}
Run Code Online (Sandbox Code Playgroud)

如何从一个活动发送SharedBooking列表并在其他活动中接收该列表?

请建议我任何可用的链接或示例代码.

Ruz*_*zin 63

首先,使列表的类实现Serializable.

public class Object implements Serializable{}
Run Code Online (Sandbox Code Playgroud)

然后你可以将列表转换为(Serializable).像这样:

List<Object> list = new ArrayList<Object>();
myIntent.putExtra("LIST", (Serializable) list);
Run Code Online (Sandbox Code Playgroud)

要检索您执行的列表:

Intent i = getIntent();
list = (List<Object>) i.getSerializableExtra("LIST");
Run Code Online (Sandbox Code Playgroud)

而已.

  • 最好的办法.通过不实施Parcelable节省了很多时间:) Ty! (3认同)
  • 发生错误“Parcelable 在 startActivity 中遇到 IOException 写入可序列化对象” (3认同)

Vin*_*kla 51

使用parcelable.以下是您将如何做到这一点:

public class SharedBooking implements Parcelable{

    public int account_id;
    public Double betrag;
    public Double betrag_effected;
    public int taxType;
    public int tax;
    public String postingText;

    public SharedBooking() {
        account_id = 0;
        betrag = 0.0;
        betrag_effected = 0.0;
        taxType = 0;
        tax = 0;
        postingText = "";
    }

    public SharedBooking(Parcel in) {
        account_id = in.readInt();
        betrag = in.readDouble();
        betrag_effected = in.readDouble();
        taxType = in.readInt();
        tax = in.readInt();
        postingText = in.readString();
    }

    public int getAccount_id() {
        return account_id;
    }
    public void setAccount_id(int account_id) {
        this.account_id = account_id;
    }
    public Double getBetrag() {
        return betrag;
    }
    public void setBetrag(Double betrag) {
        this.betrag = betrag;
    }
    public Double getBetrag_effected() {
        return betrag_effected;
    }
    public void setBetrag_effected(Double betrag_effected) {
        this.betrag_effected = betrag_effected;
    }
    public int getTaxType() {
        return taxType;
    }
    public void setTaxType(int taxType) {
        this.taxType = taxType;
    }
    public int getTax() {
        return tax;
    }
    public void setTax(int tax) {
        this.tax = tax;
    }
    public String getPostingText() {
        return postingText;
    }
    public void setPostingText(String postingText) {
        this.postingText = postingText;
    }
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(account_id);
        dest.writeDouble(betrag);
        dest.writeDouble(betrag_effected);
        dest.writeInt(taxType);
        dest.writeInt(tax);
        dest.writeString(postingText);

    }

    public static final Parcelable.Creator<SharedBooking> CREATOR = new Parcelable.Creator<SharedBooking>()
    {
        public SharedBooking createFromParcel(Parcel in)
        {
            return new SharedBooking(in);
        }
        public SharedBooking[] newArray(int size)
        {
            return new SharedBooking[size];
        }
    };

}
Run Code Online (Sandbox Code Playgroud)

传递数据:

Intent intent = new Intent(getApplicationContext(),YourActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("data", sharedBookingObject);
intent.putExtras(bundle);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

检索数据:

Bundle bundle = getIntent().getExtras();
sharedBookingObject = bundle.getParcelable("data");
Run Code Online (Sandbox Code Playgroud)

  • 您可以使用bundle.putParcelableArrayList(key,value)来设置和bundle.getParcelableArrayList(key)来获取列表. (13认同)
  • 很好的答案!将此作为我自己的应用程序的示例. (2认同)

Sha*_*amS 9

可分配的对象类

    public class Student implements Parcelable {

        int id;
        String name;

        public Student(int id, String name) {
            this.id = id;
            this.name = name;

        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }


        @Override
        public int describeContents() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int arg1) {
            // TODO Auto-generated method stub
            dest.writeInt(id);
            dest.writeString(name);
        }

        public Student(Parcel in) {
            id = in.readInt();
            name = in.readString();
        }

        public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {
            public Student createFromParcel(Parcel in) {
                return new Student(in);
            }

            public Student[] newArray(int size) {
                return new Student[size];
            }
        };
    }
Run Code Online (Sandbox Code Playgroud)

和清单

ArrayList<Student> arraylist = new ArrayList<Student>();
Run Code Online (Sandbox Code Playgroud)

来自呼叫活动的代码

Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("mylist", arraylist);
intent.putExtras(bundle);       
this.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

被叫活动的代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);   

    Bundle bundle = getIntent().getExtras();
    ArrayList<Student> arraylist = bundle.getParcelableArrayList("mylist");
}
Run Code Online (Sandbox Code Playgroud)