【发布时间】:2019-08-13 06:36:03
【问题描述】:
我在 Android 中有 2 个下拉菜单。我想根据在第一个下拉列表中选择的值更改第二个下拉列表的内容。这是代码。
<string-array name="categoriesSpinner">
<item>ACCESS</item>
<item>AVAILABILITY - PERFORMANCE</item>
<item>FUNCTIONALITY</item>
<item>INQUIRY</item>
<item>DATA ERROR</item>
<item>ERROR MESSAGE</item>
</string-array>
UI
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/categorySpinner"
android:layout_width="match_parent"
android:layout_height="45sp"
android:hint="Category"
android:layout_marginTop="@dimen/margin_small"
android:background="@drawable/bg_drawable" />
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/subcategorySpinner"
android:layout_width="match_parent"
android:layout_height="45sp"
android:hint="Sub-category"
android:layout_marginTop="@dimen/margin_small"
android:background="@drawable/bg_drawable" />
Java:
public AppCompatSpinner mTextView, getmTextView;
//AppCompatSpinner
mTextView = findViewById(R.id.categorySpinner);
String[] categories = getResources().getStringArray(R.array.categoriesSpinner);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, categories);
arrayAdapter.notifyDataSetChanged();
mTextView.setAdapter(arrayAdapter);
String option = String.valueOf(mTextView.getSelectedItem());
getmTextView = findViewById(R.id.subcategorySpinner);
if (option.contentEquals("ACCESS")) {
List<String> list = new ArrayList<>();
list.add("ACCOUNT LOCKED");
list.add("RESET PASSWORD");
ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, list);
arrayAdapter1.notifyDataSetChanged();
getmTextView.setAdapter(arrayAdapter1);
}
if (option.contentEquals("AVAILABILITY - PERFORMANCE")) {
List<String> list = new ArrayList<>();
list.add("LIMITED - DEGRADED");
list.add("UNAVILABLE - DOWN");
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, list);
stringArrayAdapter.notifyDataSetChanged();
getmTextView.setAdapter(stringArrayAdapter);
}
当我在我的 android 设备中运行代码时,单击第一个下拉列表中的第二个值时,第二个下拉列表的值不会改变。我该如何解决?
【问题讨论】:
-
在第一个微调器上使用
onItemSelected侦听器并相应地更新第二个微调器适配器列表。