我正在開發android應用程序.I將Fragment類擴展到我的應用程序中的所有java類。我想從片段發送數據到另一個片段(AllDeals-> CarDetails).For發送數據我創建parcelable類對象,並將此對象發送到另一個fragment.But在新的片段,它總是顯示空數據。我嘗試了很多時間,但我沒有成功。我已經很多次在這個,所以我決定發佈問題。所以我請你解決我的問題。 我初始化CarBean像發送到另一個片段後,捆綁的對象顯示爲空android
obj = new CarBean();
obj.setName("deepak");
obj.setPrize(txt_carprize.getText().toString());
obj.setAvgPrize(txt_vat.getText().toString());
obj.setImage("R.drawable.img"+currentposition);
這裏是我的代碼
public class AllDeals extends BaseFragment implements OnClickListener
{
android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
BaseFragment fragment = new AllDeals();
bundle.putParcelable("cardetails", obj);
fragment.setArguments(bundle);
fragmentTransaction.commit();
}
這個類我讀了捆綁對象
public class CarDetails extends BaseFragment implements OnClickListener
{
Bundle bundledata=getArguments();
CarBean carbean=bundledata.getParcelable("cardetails");
String name=carbean.getName();
}
,這裏是Java bean類
public class CarBean implements Parcelable
{
String name,prize,avgprize,image;
public CarBean(Parcel in)
{
name=in.readString();
prize=in.readString();
avgprize=in.readString();
image=in.readString();
}
public CarBean()
{
// TODO Auto-generated constructor stub
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setPrize(String prize)
{
this.prize=prize;
}
public String getPrize()
{
return prize;
}
public void setAvgPrize(String avgprize)
{
this.avgprize=avgprize;
}
public String getAvgPrize()
{
return avgprize;
}
public void setImage(String image)
{
this.image=image;
}
public String getImage()
{
return image;
}
@Override
public int describeContents()
{
// TODO Auto-generated method stub
return 0;
}
/*
here we store the data into parcle object
*/
@Override
public void writeToParcel(Parcel out, int flags)
{
// TODO Auto-generated method stub
out.writeString(name);
out.writeString(prize);
out.writeString(avgprize);
out.writeString(image);
}
/* retrive the data from parcel object */
public static final Parcelable.Creator<CarBean> CREATOR = new Parcelable.Creator<CarBean>()
{
@Override
public CarBean createFromParcel(Parcel in)
{
// TODO Auto-generated method stub
/* this method call CarBean Constructor */
return new CarBean(in);
}
@Override
public CarBean[] newArray(int size)
{
// TODO Auto-generated method stub
return new CarBean[size];
}
};
}
代碼看起來不完整或放錯位置。將它插入到捆綁中時,確定該對象是否爲空? – Jitsu 2014-08-27 11:37:42
已經初始化CarBean對象,像obj = new CarBean(); obj.setName(「deepak」); obj.setPrize(txt_carprize.getText()。toString()); obj.setAvgPrize(txt_vat.getText()。toString()); obj.setImage(「R.drawable.img」+ currentposition); – user3458375 2014-08-27 11:39:36