I have an array: Object[] array
, and an array adapter that extends ArrayAdapter<Object>
.
When i try to delete from it using adapter.remove(Object obj)
i get an UnsupportedOperationException
exception, just as this post.
The provided answers suggest using an ArrayList instead. This is not an option for me. I need to stick with array. So i copied the array to another array, but without the item i want to delete. Then i just did:
oldArray = newArray;
and then called adapter.notifyDataSetChanged();
.
This works fine except it doesn't refresh the screen. I need to close then reopen the screen to see the changes. Shouldn't notifyDataSetChanged()
do the refreshing?
edit:
Following @MD's answer, this is what i'm doing right now:
controller.onRemove(id);
public void onRemove(int id) {
int userListLength = usersArray.length;
int j = 0;
User[] newUserList = new User[userListLength-1];
for(int i=0; i<userListLength; i++)
{
if(id != usersArray[i].getId())
{
newUserList[j] = new User();
newUserList[j] = usersArray[i];
j++;
}
}
usersArray = newUserList;
//store in database
//...
view.getAdapter().refresh( usersArray );
}
public void refresh(User[] items)
{
this.userArray = items;
notifyDataSetChanged();
}
adapter construction:
adapter = new myUserAdapter( controller.getBaseContext(), R.layout.user_row, userArrayList);
usersListView.setAdapter( adapter );
and in myUserAdapter
i have:
private User[] userArray;
Solution:
@MD's answer works. But I also had to override getCount()
in the adapter:
@Override
public int getCount () {
return userArray.length;
}
It's explained in the accepted answer here.