在Android中创建动态Spinner

时间:2015-10-04 06:09:55

标签: android android-studio android-spinner

我想创建三个微调器。在第一个Spinner中,我必须显示国家名称,并根据国家名称的选择,我必须在第二个Spinner中加载该国家的州名称,并根据州的选择,我必须在Third Spinner中加载该州的城市名称。任何人都可以发布任何示例,并建议哪种技术更好地定义Java或Xml中的微调数据。

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, locations);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(dataAdapter);

1 个答案:

答案 0 :(得分:5)

布局xml

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

<!-- Label -->

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="8dip"
    android:text="@string/lblAcc" />

<!-- Spinner Dropdown -->

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dip"
    android:layout_marginRight="8dip"
    android:layout_marginTop="10dip"
    android:entries="@array/acc_type" />

<!-- Select Label -->

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="8dip"
    android:text="@string/lblSubAcc" />

<!-- Spinner Dropdown -->
<Spinner
    android:id="@+id/spinner2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:layout_marginLeft="8dip"
    android:layout_marginRight="8dip"
/>

资源xml应该如下

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">Spinner Example</string>
<string name="action_settings">Settings</string>
<string name="lblAcc">Select Account Type</string>
<string name="lblSubAcc">Select Account Head</string>

<string-array name="acc_type">
    <item>Income</item>
    <item>Expense</item>
</string-array>

</resources>

要使用的Java类

public class SpinnerEx4Activity extends Activity implements
OnItemSelectedListener{
Spinner s1,s2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spinner_ex4);
    s1 = (Spinner)findViewById(R.id.spinner1);
    s2 = (Spinner)findViewById(R.id.spinner2);
    s1.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    String sp1= String.valueOf(s1.getSelectedItem());
    Toast.makeText(this, sp1, Toast.LENGTH_SHORT).show();
    if(sp1.contentEquals("Income")) {
        List<String> list = new ArrayList<String>();
        list.add("Salary");//You should add items from db here (first spinner)

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
          dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapter.notifyDataSetChanged();
        s2.setAdapter(dataAdapter);
    }
    if(sp1.contentEquals("Expense")) {
        List<String> list = new ArrayList<String>();
        list.add("Conveyance");//you should add items from db here(2nd spinner)

        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
          dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapter2.notifyDataSetChanged();
        s2.setAdapter(dataAdapter2);
    }

}

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

}

使用if else阶梯,您可以添加更多的微调器,如第3个微调器,具体取决于第2个,依此类推。