how to delete the particular selected item from the list of items in spinner

别等时光非礼了梦想. 提交于 2019-12-10 11:55:33

问题


I followed http://www.designerandroid.com/?p=8 this one to add the values in the spinner.In it while we select the "clear spinner items" the whole events are will be delete. But i need to the selected particular item only want to delete.Any one can help me.. The sample code will help to me lot.


回答1:


To delete particular item from spinner you have to remove it from arrayadapter which you are using for filling it.

So first get the position of item you are wanting to delete.

Then get the object from arrayadapter from its position by method.

int pos = 0;object t=m_adapterForSpinner.getitem(pos);// where m_arrayadapter is array adapter using for filling spinner

And then remove it from spinner by using following code:

m_adapterForSpinner.remove((CharSequence) t);

Then fill your spinner again with arrayadapter.




回答2:


If you want to remove the selected item in the spinner:

adapter.remove((String)spinner.getSelectedItem());
adapter.notifyDataSetChanged();

where "adapter" is the adapter set to the spinner, it's as simple as that. :)

if adapter is out of scope you can get the adapter from the spinner, cast properly and remove the item:

((List<String>) spinner.getAdapter()).remove((String)spinner.getSelectedItem());
((List<String>) spinner.getAdapter()).notifyDataSetChanged();


来源:https://stackoverflow.com/questions/4778745/how-to-delete-the-particular-selected-item-from-the-list-of-items-in-spinner

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!