旋转器不会从我的阵列中填充

时间:2014-05-27 14:10:32

标签: android eclipse android-fragments android-arrayadapter android-spinner

我的应用程序通过点击按钮将我重定向到我家的不同屏幕。有一次,我到达我的城市屏幕,我的微调器是可见的。但它不包含我的数组中的任何元素。没有任何力量关闭或任何东西,但它不起作用。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_city);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
    }       
}

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

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

    String[] test = {"test1", "test2"};
    Spinner spin = (Spinner) v.findViewById(R.id.countries);
    ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, test);
    adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin.setAdapter(adapt);
    return v;
}

1 个答案:

答案 0 :(得分:0)

您需要具备以下内容。

 public static PlaceHolderFragment extends Fragment
   {
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

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

    String[] test = {"test1", "test2"};
    Spinner spin = (Spinner) v.findViewById(R.id.countries);
    ArrayAdapter<String> adapt = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, test);
    adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin.setAdapter(adapt);
    return v;
    }
   }

还可以使用getActivity()来获取托管活动的上下文。