传递对象的列表从一个活动到Android的其他活动 [英] Pass list of objects from one activity to other activity in android
本文介绍了传递对象的列表从一个活动到Android的其他活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从另一个活动传递对象的列表从一个活动。我有一个类SharedBooking下面: -
i want to pass a list of objects from one activity from another activity . I have one class SharedBooking Below :-
public class SharedBooking {
public int account_id;
public Double betrag;
public Double betrag_effected;
public int taxType;
public int tax;
public String postingText;
}
从调用活动
code: -
Code from Calling activity :-
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);
}
在所谓的活动
code: -
COde on called activity :-
Bundle extras = getIntent().getExtras();
if (extras != null)
{
amount=extras.getDouble(Constants.BETRAG,0);
}
我如何从一个活动发送SharedBooking的列表,并接受她上的其它活动。
How i send list of SharedBooking from one activity , and recieve that on other activity .
请给我建议任何可用的链接或样品code。
Please suggest me any usable link or sample code .
推荐答案
使用 parcelable 。这里是你将如何做到这一点:
Use parcelable. Here is how you will do it:
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];
}
};
}
传递数据:
Intent intent = new Intent(getApplicationContext(),YourActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("data", sharedBookingObject);
intent.putExtras(bundle);
startActivity(intent);
检索数据:
Bundle bundle = getIntent().getExtras();
sharedBookingObject = bundle.getParcelable("data");
这篇关于传递对象的列表从一个活动到Android的其他活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文