我使用newInstance方法初始化片段。然后我尝试在onCreate方法中获取参数。
public class ShowCostsFragment extends Fragment {
public static final String TAG = "ShowCostsFragment";
private static final String ARG_COSTS = "costs";
private List<Cost> mCosts;
public ShowCostsFragment() {
}
public static ShowCostsFragment newInstance(List<Cost> costs) {
ShowCostsFragment fragment = new ShowCostsFragment();
Bundle args = new Bundle();
args.putParcelableArrayList(ARG_COSTS, (ArrayList<? extends Parcelable>) costs);
fragment.setArguments(args);
Log.d(TAG, "newInstance(). Costs size: " + costs.size());
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mCosts = getArguments().getParcelableArrayList(ARG_COSTS);
Log.d(TAG, "onCreate(). Costs size: " + mCosts.size());
}
}
在MainActivity中,我运行此片段以显示一些数据。
private void startShowCostsFragment() {
List<Cost> costs = mDBHelper.getAllCosts();
Log.d(TAG, "startShowCostsFragment(). Costs size: " + costs.size());
Fragment showCostsFragment = ShowCostsFragment.newInstance(costs);
showFragment(showCostsFragment);
}
private void showFragment(Fragment fragment) {
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right,
android.R.anim.slide_in_left, android.R.anim.slide_out_right)
.replace(R.id.content_main, fragment)
.addToBackStack(null)
.commit();
}
当我运行方法startShowCostsFragment时,片段中的第二个和后续时间参数未正确设置。虽然新值已传递给方法,但mCosts列表不会更改其值。有时onCreate方法根本不会调用。 当片段动态替换多次时,如何正确地将参数放入片段?
成本等级:
public class Cost implements Parcelable {
private int id;
private int value;
private String date;
private String description;
private List<Tag> tags = new ArrayList<>();
public Cost(int id, String date, int value, String description, List<Tag> tags) {
this.id = id;
this.value = value;
this.date = date;
this.description = description;
this.tags = tags;
}
public Cost(int value, String date, String description, List<Tag> tags) {
this.value = value;
this.date = date;
this.description = description;
this.tags = tags;
}
public Cost() {
}
public int getId() {
return id;
}
public int getValue() {
return value;
}
public String getDate() {
return date;
}
public String getDescription() {
return description;
}
public List<Tag> getTags() {
return tags;
}
public void setId(int id) {
this.id = id;
}
public void setValue(int value) {
this.value = value;
}
public void setDate(String date) {
this.date = date;
}
public void setDescription(String description) {
this.description = description;
}
public void setTags(List<Tag> tags) {
this.tags = tags;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeInt(this.value);
dest.writeString(this.date);
dest.writeString(this.description);
dest.writeTypedList(this.tags);
}
protected Cost(Parcel in) {
this.id = in.readInt();
this.value = in.readInt();
this.date = in.readString();
this.description = in.readString();
this.tags = in.createTypedArrayList(Tag.CREATOR);
}
public static final Creator<Cost> CREATOR = new Creator<Cost>() {
@Override
public Cost createFromParcel(Parcel source) {
return new Cost(source);
}
@Override
public Cost[] newArray(int size) {
return new Cost[size];
}
};
}
答案 0 :(得分:0)
Cost
类必须实现Parcelable
才能将其作为捆绑包发送。
编辑:
如果您的Cost
班级正在实施Parcelable
,那么您就不需要像(ArrayList<? extends Parcelable>) costs
那样进行投射。
你做演员,而不是实施。
public class Cost implement Parcelable {
和实施。看看here。