创建一个子微调器

时间:2014-02-04 09:47:31

标签: android spinner android-spinner

我正在尝试在嵌入式微调器中创建一个子微调器。我的目标是,如果在主微调器中选择了一个选项,那么根据所选选项,必须显示子微调器选项。 / p>

在我的项目中,我可以选择在主旋转器中选择状态。因此,根据所选状态,我必须能够在子微调器中显示区域。我已经成功实现了主微调器,但我应该怎么做以便根据所选的状态显示区域(在子微调器中)。

region_slection.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

          <Spinner
            android:id="@+id/spinner1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="0dp"
            android:layout_y="0dp" />

          <Spinner
              android:id="@+id/spinner2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_x="0dp"
              android:layout_y="98dp" 
              android:entries="@array/states_array"/>

          <Button
              android:id="@+id/btn1"
              android:layout_width="106dp"
              android:layout_height="wrap_content"
              android:layout_x="100dp"
              android:layout_y="220dp"
              android:text="Submit" />

    </AbsoluteLayout>

Region_selection.java

    public class Region_selection extends Activity {

    private Spinner spinner1,spinner2;
    private Button btn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.region_selection);

        addListenerOnSpinnerItemSelection();
        addListenerOnButton();

    }

    public void addListenerOnSpinnerItemSelection(){

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
    }

    //get the selected dropdown list value
        public void addListenerOnButton() {

            spinner1 = (Spinner) findViewById(R.id.spinner1);
            spinner2 = (Spinner) findViewById(R.id.spinner2);

            btn1 = (Button) findViewById(R.id.btn1);

            btn1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    Toast.makeText(Region_selection.this,
                            "OnClickListener : " + 
                            "\nSpinner 1 : " + String.valueOf(spinner1.getSelectedItem()) +
                            "\nSpinner 2 : " + String.valueOf(spinner2.getSelectedItem()),
                            Toast.LENGTH_SHORT).show();
                }

            });

        }
}

CustomOnItemSelectedListener.java

    public class CustomOnItemSelectedListener implements OnItemSelectedListener {

    //private static final String TAG = null;
    final Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos,
            long id) {
        // TODO Auto-generated method stub

        ArrayAdapter<CharSequence> adapterDistrict;
        String state_selected;

        switch (parent.getId()) {
        case R.id.spinner1:     //state spinner(parent spinner)
            Log.e(TAG, parent.getAdapter().getItem(pos).toString());
            state_selected = parent.getAdapter().getItem(pos).toString();

            if(state_selected.equals("Kerala"))
            {
                adapterDistrict = ArrayAdapter.createFromResource(this, R.array.kerala_districts, R.layout.region_selection);
            }


            else
            {
                adapterDistrict = ArrayAdapter.createFromResource(this, "Hai", R.layout.region_selection);
            }
            adapterDistrict.setDropDownViewResource(R.layout.region_selection);
            spinner2.setAdapter(adapterDistrict);
            break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

}

我在使用TAG变量时遇到错误并且使用createFromResource时出错。错误说“ArrayAdapter类型中的方法createFromResource(context,int,int)不适用于参数(CustomOnItemListner,int,int)

我也不知道为什么我在CustomOnItemSelectedListener.java中使用findViewById会出错。

1 个答案:

答案 0 :(得分:0)

覆盖onItemSelected以达到您的要求。 activity / class应该实现OnItemSelectedListener。

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
        long id) {
    ArrayAdapter<CharSequence> adapterCity; 
    String mCountrySelected;
      switch(parent.getId()) {
        case R.id.create_sp_country:  // parent spinner id
            Log.e(TAG,parent.getAdapter().getItem(pos).toString());
            mCountrySelected = parent.getAdapter().getItem(pos).toString();

            if(mCountrySelected.equals("United Kingdom"))
            {
                adapterCity = ArrayAdapter.createFromResource(this,
                        R.array.United_Kingdom_Cities, android.R.layout.simple_spinner_item);
            }
            else
            {
                adapterCity = ArrayAdapter.createFromResource(this,
                        R.array.create_empty_array, android.R.layout.simple_spinner_item);
            }

            adapterCity.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            createSpCity.setAdapter(adapterCity); // Where createSpCity is the child spinner
            break;

        }
}