问题是我多次调用Fragment类(每个人都有不同的参数),然后我需要回忆一下这些参数进行一些处理。但是,每当我需要它时调用这个参数,每个片段都是一样的,即使我用不同的参数明确地调用它!
这里有一些代码可以帮助你们理解这个问题。
NewStudentActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_student);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
fab = (FloatingActionButton) findViewById(R.id.fab_new_student);
UserDataFragment frag = new UserDataFragment();
frag.show(getSupportFragmentManager(), "user_data");
measurementList = new SparseArray<>(4);
//...
private void setupViewPager (ViewPager viewPager){
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
if (calcMethod == Utility.PETROVSKI){
if (userGender == Utility.GENDER_MALE){
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_SUBESCAPULAR),
Utility.MEASUREMENT_SUBESCAPULAR);
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_TRICIPITAL),
Utility.MEASUREMENT_TRICIPITAL);
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_SUPRAILIACA),
Utility.MEASUREMENT_SUPRAILIACA);
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_PANTURRILHA),
Utility.MEASUREMENT_PANTURRILHA);
} else if (userGender == Utility.GENDER_FEMALE) {
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_AXILAR),
Utility.MEASUREMENT_AXILAR);
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_SUPRAILIACA),
Utility.MEASUREMENT_SUPRAILIACA);
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_COXA),
Utility.MEASUREMENT_COXA);
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_PANTURRILHA),
Utility.MEASUREMENT_PANTURRILHA);
} else {
finish();
}
} else if (calcMethod == Utility.GUEDES) {
if (userGender == Utility.GENDER_MALE){
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_SUBESCAPULAR),
Utility.MEASUREMENT_SUBESCAPULAR);
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_ABDOMINAL),
Utility.MEASUREMENT_ABDOMINAL);
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_SUPRAILIACA),
Utility.MEASUREMENT_SUPRAILIACA);
} else if (userGender == Utility.GENDER_FEMALE) {
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_COXA_PROXIMAL),
Utility.MEASUREMENT_COXA_PROXIMAL);
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_SUPRAILIACA),
Utility.MEASUREMENT_SUPRAILIACA);
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_SUBESCAPULAR),
Utility.MEASUREMENT_SUBESCAPULAR);
} else {
finish();
}
} else {
finish();
}
viewPager.setAdapter(adapter);
}
ViewPagerAdapter:
class ViewPagerAdapter extends FragmentPagerAdapter{
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager){
super(manager);
}
@Override
public Fragment getItem(int position){
return fragmentList.get(position);
}
@Override
public int getCount(){
return fragmentList.size();
}
public void addFragment (Fragment fragment, String title){
fragmentList.add(fragment);
fragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}
}
MeasurementFragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
view = inflater.inflate(R.layout.fragment_measurement, container, false);
measurement1 = (EditText) view.findViewById(R.id.te_first_measurement);
measurement2 = (EditText) view.findViewById(R.id.te_second_measurement);
measurement3 = (EditText) view.findViewById(R.id.te_third_measurement);
median = (EditText) view.findViewById(R.id.mediana);
median.setKeyListener(null);
btn_check = (AppCompatButton) view.findViewById(R.id.btn_check_measurements);
btn_check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
returnValues();
}
});
return view;
}
public static MeasurementFragment newInstance(String measurement){
MeasurementFragment frag = new MeasurementFragment();
Bundle args = new Bundle();
arg = measurement;
args.putString(ARG_MEASUREMENT, measurement);
frag.setArguments(args);
return frag;
}
private void returnValues(){
if (checkMedian()){
Bundle data = new Bundle();
receiver = (MeasurementFinished) getActivity();
data.putString("measurement", arg);
data.putDouble("mediana", median_value);
data.putDouble("measurement1", first_measurement_value);
data.putDouble("measurement2", second_measurement_value);
data.putDouble("measurement3", third_measurement_value);
receiver.onMeasurementFinished(data);
}
}
答案 0 :(得分:1)
您没有在arg
和newInstance
中显示returnValues
的声明,但由于您在静态方法中使用它,因此它必须是静态字段。
这意味着每个班级只有一个,所以当你说arg = measurement
时,你会一遍又一遍地为同一个字段分配不同的值。这就是为什么你只看到一个价值。
试试这个:
public static MeasurementFragment newInstance(String measurement){
MeasurementFragment frag = new MeasurementFragment();
Bundle args = new Bundle();
// don't use the static arg here
args.putString(ARG_MEASUREMENT, measurement);
frag.setArguments(args);
return frag;
}
private void returnValues(){
if (checkMedian()){
Bundle data = new Bundle();
receiver = (MeasurementFinished) getActivity();
// this is how you get a value set with setArguments
String measurement = getArguments().getString(ARG_MEASUREMENT);
data.putString("measurement", measurement);
data.putDouble("mediana", median_value);
data.putDouble("measurement1", first_measurement_value);
data.putDouble("measurement2", second_measurement_value);
data.putDouble("measurement3", third_measurement_value);
receiver.onMeasurementFinished(data);
}
}