当项目在 strings.xml 中列出而不是在 Android Studio 中静态时,如何删除按钮单击时在微调器中选择的项目?
How to delete the item selected in a Spinner on button click when the items are listed in strings.xml and not as static in Android Studio?
我在删除微调器中 selected 的项目时遇到问题。问题是我已经在字符串中声明了数组列表,如下所示。问题是当我 select 来自微调器的一个项目并按下按钮,比如删除按钮时,当我再次使用该微调器时,该项目应该从数组列表中删除并且不应该显示在微调器下拉列表中。我经历了几个例子,但在所有这些例子中,项目在 main activity 本身内部被声明为静态的,但我在字符串中声明了它们。另一件事是我在 FRAGMENT 中有按钮单击事件。如果有人用代码帮助我,那将是一个很大的帮助。对不起,如果我有任何错误。提前致谢。
<string name="relation_spinner">Relation</string>
<string-array name="relation_array">
<item>--Select--</item>
<item>Father</item>
<item>Mother</item>
<item>Son</item>
<item>Daughter</item>
<item>Brother</item>
<item>Sister</item>
</string-array>
自己找的!
我们实际上应该通过适配器删除它。
代码如下:
if (selectedText.equals("Father")) {
yourAdapter.remove(selectedText);
yourAdapter.notifyDataSetChanged();
}
And/or如果你想为多个项目指定,你可以这样做:
if (selectedText.equals("Father") || selectedText.equals("Mother")) {
yourAdapter.remove(selectedText);
yourAdapter.notifyDataSetChanged();
}
我在删除微调器中 selected 的项目时遇到问题。问题是我已经在字符串中声明了数组列表,如下所示。问题是当我 select 来自微调器的一个项目并按下按钮,比如删除按钮时,当我再次使用该微调器时,该项目应该从数组列表中删除并且不应该显示在微调器下拉列表中。我经历了几个例子,但在所有这些例子中,项目在 main activity 本身内部被声明为静态的,但我在字符串中声明了它们。另一件事是我在 FRAGMENT 中有按钮单击事件。如果有人用代码帮助我,那将是一个很大的帮助。对不起,如果我有任何错误。提前致谢。
<string name="relation_spinner">Relation</string>
<string-array name="relation_array">
<item>--Select--</item>
<item>Father</item>
<item>Mother</item>
<item>Son</item>
<item>Daughter</item>
<item>Brother</item>
<item>Sister</item>
</string-array>
自己找的! 我们实际上应该通过适配器删除它。 代码如下:
if (selectedText.equals("Father")) {
yourAdapter.remove(selectedText);
yourAdapter.notifyDataSetChanged();
}
And/or如果你想为多个项目指定,你可以这样做:
if (selectedText.equals("Father") || selectedText.equals("Mother")) {
yourAdapter.remove(selectedText);
yourAdapter.notifyDataSetChanged();
}