0

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.