从列表中,用户可以点击和项目,片段将膨胀显示所点击项目的数据,用户也可以编辑该数据并单击保存以保存编辑的数据。
但是,如果用户想要创建新对象,则从包含列表的屏幕也是一个添加按钮。
当用户点击列表中的某个项目时,会调用newInstance(..);
并在片段onCreateView();
中,我为不同视图中的被点击项目初始化所有变量。但这并不顺利,因为我一直在努力:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String, int)' on a null object reference
从RecyclerView Adapter onClick()调用newInstance:
public static CreateTrainingFragment newInstance(ItemModel itemModel) {
bundle = new Bundle();
bundle.putInt(SB_DURATION, itemModel.getDuration());
bundle.putInt(SB_DISTANCE, itemModel.getDistance());
CreateTrainingFragment createTrainingFragment = new CreateTrainingFragment();
createTrainingFragment.setArguments(bundle);
return createTrainingFragment;
}
这里我使用getArguments();并将参数提供给Views:
如果参数不存在,默认的0变量是否会自动插入到我的sbduration.setProgress();
中?
private void initArgumentsData() {
sbduration.setProgress(getArguments().getInt(SB_DURATION, 0));
sbDistance.setProgress(getArguments().getInt(SB_DISTANCE, 0));
txtduration.setText(getArguments().getInt(SB_DURATION, 0) + " min");
txtDistance.setText(getArguments().getInt(SB_DISTANCE, 0) + " km");
}
以下是我的视图创建方式以及我在哪里使用InitArgumentData();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.createtraining_layout, container, false);
sbduration = (SeekBar) v.findViewById(R.id.seekbar_time);
sbDistance = (SeekBar) v.findViewById(R.id.seekbar_distance);
txtduration = (TextView) v.findViewById(R.id.time_value);
txtDistance = (TextView) v.findViewById(R.id.distance_value);
sbduration.setMax(100);
sbDistance.setMax(50);
initArgumentsData();
}
从RecyclerView我开始这样一个新的片段实例:
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ItemModel itemModel = realm.getDefaultInstance().where(ItemModel.class).equalTo("timestamp",list.get(getAdapterPosition()).getTimestamp()).findFirst();
CreateTrainingFragment createTrainingFragment = CreateTrainingFragment.newInstance(itemModel, true);
fragmentManager.beginTransaction().replace(R.id.navdrawer_maincontainer,createTrainingFragment).addToBackStack(null).commit();
}
});
从添加按钮开始,片段就像这样:
addbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().beginTransaction().replace(R.id.navdrawer_maincontainer,new CreateTrainingFragment()).addToBackStack(null).commit();
}
});
答案 0 :(得分:0)
getFragmentManager().beginTransaction().replace(R.id.navdrawer_maincontainer,new CreateTrainingFragment()).addToBackStack(null).commit();
在这里,您使用的是new CreateTrainingFragment()
。因此,您没有收到捆绑包,因为没有附加捆绑包。您应首先调用newInstance
方法获取CreateTrainingFragment的对象,然后将其置于替换状态。
ItemModel itemModel = realm.getDefaultInstance().where(ItemModel.class).equalTo("timestamp",list.get(getAdapterPosition()).getTimestamp()).findFirst();
CreateTrainingFragment createTrainingFragment = CreateTrainingFragment.newInstance(itemModel, true);
getFragmentManager().beginTransaction().replace(R.id.navdrawer_maincontainer, createTrainingFragment).addToBackStack(null).commit();
答案 1 :(得分:0)
你怎么检查参数是否存在?
private void initArgumentsData() {
Bundle args = getArguments();
int duration = 0;
int distance = 0;
if (args != null) {
duration = args.getInt(SB_DURATION, 0);
distance = args.getInt(SB_DISTANCE, 0);
}
sbduration.setProgress(duration);
sbDistance.setProgress(distance);
txtduration.setText(duration + " min");
txtDistance.setText(distance + " km");
}
即使您在片段上调用了newInstance
,您仍然需要为该方法提供new ItemModel
。