gpt4 book ai didi

android - 在 Android 中创建动态 Spinner

转载 作者:行者123 更新时间:2023-11-29 15:09:54 25 4
gpt4 key购买 nike

我想创建三个微调器。在第一个 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);

最佳答案

布局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 梯形图,您可以添加更多微调器,例如第三个微调器,具体取决于第二个微调器,依此类推。

关于android - 在 Android 中创建动态 Spinner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32930559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com