14

我正在尝试从我的以下片段中的一个字符串数组中设置我的微调器的 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);

}

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

4

1 回答 1

38

问题是我正在返回一个新视图,而不是我设置的那个。我不得不这样做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;

}
于 2012-09-02T19:18:22.913 回答