简体   繁体   中英

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?

I am facing a problem in deleting an item that is selected in a spinner. The thing is I've declared the array list inside strings like given below. The thing is when I select an item from the spinner and press the button,say delete button, the item should get deleted from the array list and should not show in the spinner dropdown, when I use that spinner again. I went through several examples, but in all those, the items are declared as static inside main activity itself, but I've declared them inside strings. Another thing is I have the button click event inside a FRAGMENT. It would be a great help if somebody help me with a code. Sorry if I am wrong with any terms. Thanks in advance.

<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>

Found it myself! We should actually remove it via adapter. The code goes below:

if (selectedText.equals("Father")) {
                    yourAdapter.remove(selectedText);
                    yourAdapter.notifyDataSetChanged();
                }

And/or if you would like to specify for multiple items, you can do this way:

if (selectedText.equals("Father") || selectedText.equals("Mother")) {
                    yourAdapter.remove(selectedText);
                    yourAdapter.notifyDataSetChanged();
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM