在片段中设置微调器

时间:2012-09-02 17:25:19

标签: android android-fragments spinner

我正在尝试从onCreateViewpublic final class Manual extends Fragment的一个片段中的字符串数组设置我的微调器的值:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.manual, container, false);

    String [] values = 
        {"Time at Residence","Under 6 months","6-12 months","1-2 years","2-4 years","4-8 years","8-15 years","Over 15 years",};
    Spinner spinner = (Spinner) v.findViewById(R.id.spinner1);
    ArrayAdapter<String> LTRadapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values);
    LTRadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    spinner.setAdapter(LTRadapter);

    return inflater.inflate(R.layout.manual, container, false);

}

我的代码没有错误,但它没有设置微调器。微调器保持空白,没有值。关于为什么我的代码没有设置微调器的任何想法?

1 个答案:

答案 0 :(得分:28)

问题是我正在返回一个新视图,而不是我设置的视图。我必须return v;并且它工作正常。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.manual, container, false);

    String [] values = 
        {"Time at Residence","Under 6 months","6-12 months","1-2 years","2-4 years","4-8 years","8-15 years","Over 15 years",};
    Spinner spinner = (Spinner) v.findViewById(R.id.spinner1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values);
    adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    spinner.setAdapter(adapter);

    return v;

}