切换片段时如何控制微调器项目?

时间:2017-02-20 09:34:26

标签: android android-spinner

片段B有一个有三个项目的微调器。当我将片段从A切换到B时,我想显示第二项微调器。

我尝试使用singleton对象来控制它,但它无法正常工作。

这是我的代码,非常感谢任何帮助。

我的单身对象: 公共类SetForTabPages {

int forBloodSugarSpinner;

public int getForBloodSugarSpinner() {
    return forBloodSugarSpinner;
}

public void setForBloodSugarSpinner(int forBloodSugarSpinner) {
    this.forBloodSugarSpinner = forBloodSugarSpinner;
}

public static SetForTabPages setForTabPages=null;

public static SetForTabPages getTabPosition(){
    if (setForTabPages==null){
        setForTabPages=new SetForTabPages();
        return setForTabPages;
    }else {
        return setForTabPages;
    }
}

} 我的片段A关于切换片段功能:

    @Override
        public void onClick(View view) {
            int id = view.getId();
            if (id == R.id.imageDetectionSugar) {
              sendSpinnerNumber(1);//i save the value as 1.
            ((NavigationDrawerLayout)getActivity()).switchFragment(Evaluation.newInstance());
//and switch fragment to A
            }
    }

这是我的片段A:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
if (setForTabPages.getForBloodSugarSpinner() == 1) {//get the value 1.
            Toast.makeText(getActivity(),"Enter here succeed",Toast.LENGTH_SHORT).show();//i enter here succeed
            layoutForBloodPressure.setVisibility(View.GONE);//it's not working
            layoutForBloodSugar.setVisibility(View.VISIBLE);//it's not working
            layoutForBMI.setVisibility(View.GONE);//it's not working
            //may be i should put the code about adapter for item,byt i do not know how
            compareSpinner(view);
        }

        //get spinner value
        evaluationEnterItem.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                Object item = adapterView.getItemAtPosition(position);
                spinnerItem = item.toString();
                compareSpinner(view);
                Log.d("name", spinnerItem);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });


        return view;
    }
//if i just click spinner to switch , it's working here
private View compareSpinner(View view) {

        if (spinnerItem.equals(getResources().getString(R.string.bloodPressure))) {
            layoutForBloodPressure.setVisibility(View.VISIBLE);
            layoutForBloodSugar.setVisibility(View.GONE);
            layoutForBMI.setVisibility(View.GONE);
            return view;
        } else if (spinnerItem.equals(getResources().getString(R.string.bloodSugar))) {
            layoutForBloodPressure.setVisibility(View.GONE);
            layoutForBloodSugar.setVisibility(View.VISIBLE);
            layoutForBMI.setVisibility(View.GONE);
            return view;
        } else if (spinnerItem.equals(getResources().getString(R.string.bodyWeight))) {
            layoutForBloodPressure.setVisibility(View.GONE);
            layoutForBloodSugar.setVisibility(View.GONE);
            layoutForBMI.setVisibility(View.VISIBLE);
            return view;
        } else {
            layoutForBloodPressure.setVisibility(View.GONE);
            layoutForBloodSugar.setVisibility(View.GONE);
            layoutForBMI.setVisibility(View.GONE);
            return view;
        }
    }

0 个答案:

没有答案