开发者

Android - delete from array adapter <string>

开发者 https://www.devze.com 2023-03-14 11:37 出处:网络
I am creating an ArrayAdapter<String> from a resource array. Then I would like to delete an item from the list using arrayAdapter.remove(String) method. I find the correct string using arrayAdap

I am creating an ArrayAdapter<String> from a resource array. Then I would like to delete an item from the list using arrayAdapter.remove(String) method. I find the correct string using arrayAdapter.getItem开发者_如何学Python(int position). But when I call the remove method I end up having UnsupportedOperationException and I have no clue of whats going on.

on 20th June edited

I did the operation that delete from the resource not directly the adapter and notify the adapter about deletion. I was doing the operation on public boolean onContextItemSelected(MenuItem item); after the deletion on this method, I get an

ArrayAdapter.createViewFromResource(int, View, ViewGroup, int) line: 398    

NullPointerException

I think after i delete the item from resources, something is not properly configured..


You should delete element from your array and then notify adapter:

itemCart.m_items.remove(<index of element to remove>);
this.m_adapter.notifyDataSetChanged();

P.S. Use search the question is present already.


The ArrayAdapter is rendering the contents in the resource array. You should never be trying to change the contents of the array adapter directly. Instead change the underlying resource array.

So basically just remove the item from the resource array.

If you're working with a list defined like so:

List<String> resources = YOUR RESOURCE LIST;

You can remove an element with the removeItemAt call:

resources.removeItemAt(position);

This will work in most cases. In some cases you'll be working with a list that was modified outside of an activity which is rendering a view of the list. In those cases set the ArrayAdapter's notifyOnChanged (I think. I don't have the API reference in front of me) to false... then manually call notifyDataSetChanged.

0

精彩评论

暂无评论...
验证码 换一张
取 消