Question

I am writing an android app that needs to dynamically remove players from a soccer game roster in my listActivity. The Activity has an adapter that links to an array of Strings describing each player. I then try to remove a player from the list when clicked. My problem is that whenever I use the adapters built in methods to remove I keep getting this error.

 FATAL EXCEPTION: main
 java.lang.UnsupportedOperationException
 at java.util.AbstractList.remove(AbstractList.java:638);

As far as I can tell, I don't have the right to remove items from the adapter, but I don't get why since every example I have found online seems to work with no problems. This is the code I use to try and remove a single item from the adapter.

    public void onListItemClick(ListView l, View v, int position, long id)
{
    super.onListItemClick(l, v, position, id);

    adapter.remove(adapter.getItem(position));
    adapter.notifyDataSetChanged(); //Updates adapter to new changes
}

This is the code where I tried to change the data source for the adapter and notify it of changes in hopes it would update the onscreen list.

    public void onListItemClick(ListView l, View v, int position, long id)
{
    super.onListItemClick(l, v, position, id);

    mainRoster.removePlayer(position); //removes from custom roster
    stringedRoster = mainRoster.getStringArray();  //Resets string array
    adapter.notifyDataSetChanged(); //Updates adapter to new changes

}

If anybody can shine some light on what could be going on it would be much appreciated.

Was it helpful?

Solution

if you're using a List to store your Object and display them in a ListVeiw through the adapter, you've to use :

    public void onListItemClick(ListView l, View v, int position, long id)
{
    super.onListItemClick(l, v, position, id);

    list.remove(position);
    adapter.notifyDataSetChanged(); //Updates adapter to new changes
}

This works fine

OTHER TIPS

You haven't actually said what class your Adapter is, which might be helpful. But if it's an android.widget.ArrayAdapter then it has two kinds of constructors - using an array of objects or a list of objects.

Now, you will only be allowed to remove objects from the adapter if the underlying list supports removal. Arrays in Java, as you probably know, are of fixed length (you cannot add or delete an element, only change its value). If I remember correctly the ArrayAdapter internally wraps the array you supply in an unmutable list.

Solution: supply a List instead of an Array to the ArrayAdapter constructor and MAKE SURE that list is mutable (you can use an ArrayList for example or some other implementation that supports .remove() ).

If it's another adapter subclass you're using, then it's difficult to discuss its implementation details and why it won't remove elements if we don't know what it is and how it works.

try this code... on Adapter item or btn click

`holder.cancel_btn_img.setOnClickListener(v -> {
        qualificationData.remove(position);
        notifyDataSetChanged();
    });` 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top