Why Can't One Add/Remove Items from an Arrayadapter

Remove item in ArrayAdapterString in ListView

      String[] mValues = {"Orientation", "Nombre de Chambres", "Nombre de Salle de bains", "Nombre de toilettes", "Cave", "Parking", "Garage", "Jardin"};

final ArrayList<String> list =new ArrayList<String>(Arrays.asList(mValues));
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = list.get(position);
list.remove(position);
adapter.notifyDataSetChanged();
Toast.makeText(getActivity(), "You selected : " + item, Toast.LENGTH_SHORT).show();
}
});

Deleting item from ArrayAdapter

i have a way

Add refresh method in your adapter:

public void refresh(List<String> items)
{
this.items = items;
notifyDataSetChanged();
}

and call from Activity like

yourAdapter.refresh(items); // items new arrayList or Array

How to remove items from ArrayAdapter when an EditText is changed?

But I don't know how to code the 'remove' function to remove necessary
items, as this gives me an UnsupportedOperationException

If you pass an array as the data for an ArrayAdapter that array will be transformed into an unchangeable list(this mean you can't change the size of the list by adding or removing items from it). This is what the adapter is trying to do when using remove() and it obviously fails with that exception.

The most simply solution is to use a normal list(like ArrayList) with the data instead of that array:

String[] arr = new String[]{"One", "Two", "Three", "Four", "Five"};
List<String> data = new ArrayList<String>();
for (String item : arr) {
data.add(item);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item_medication, data);
mListViewItem.setAdapter(adapter);

Android ArrayAdapter: how to delete an item from within the adapter

Instead of sending, Verhicle[] send as ArrayList, if we send as an array it can't be modified in ArrayAdapter.

public class VehicleAdapter extends ArrayAdapter<Vehicle> {

private int listType=1;

public ArrayList<Vehicle> vehicles;

public VehicleAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull ArrayList<Vehicle> objects, int listType) {
super(context, resource, objects);
this.listType = listType;
this.vehicles = objects;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

LayoutInflater layoutInflater = LayoutInflater.from(getContext());
View customView = layoutInflater.inflate(R.layout.vehicle_row, null);

final Vehicle vehicle = getItem(position);

ImageView imgVehicle = (ImageView) customView.findViewById(R.id.imgVehicle);
TextView txtTitle = (TextView) customView.findViewById(R.id.txtTitle);
TextView txtDescription = (TextView) customView.findViewById(R.id.txtDescription);

txtTitle.setText(vehicle.name);
txtDescription.setText(vehicle.short_description);
Picasso.with(getContext()).load(vehicle.picture).into(imgVehicle);

// If it's the favorites list
if(listType == 2) {
Button btnDelete = (Button) customView.findViewById(R.id.btnDelete);
btnDelete.setVisibility(View.VISIBLE);

btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
remove(vehicle);
notifyDataSetChanged();
}
});
}

return customView;
}
}

UnsupportedOperationException with ArrayAdapter.remove

It seems that this problem crops up when you initialize your ArrayAdapter with an array. Try initializing it with a List<JournalEntry>. Reference: Why can't one add/remove items from an ArrayAdapter?

Unable to modify ArrayAdapter in ListView: UnsupportedOperationException

I tried it out myself and found it didn't work, so I checked out the source code of ArrayAdapter and found out the problem: The ArrayAdapter, on being initialized by an array, converts the array into an AbstractList (List<String>) which cannot be modified.

Solution
Use an ArrayList<String> instead using an array while initializing the ArrayAdapter.

String[] array = {"a","b","c","d","e","f","g"}; 
ArrayList<String> lst = new ArrayList<String>(Arrays.asList(array));
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, lst);

How to delete item from ListView while using Custom ArrayAdapter w/ Dialog

You can do this in either way . You can create a function in your adapter class and perform the clickListener on it .

deleteItem.setOnClickListener(v -> {
potsList.remove(getAdapterPosition());
notifyDataSetChanged();
}

Or in your class , when remove the item from list , don't forget to notify the adapter . One the adapter get notified , it will reflect the change on screen.

Removing items from Android ListView/ArrayAdapter doesn't work after text filtering

It seems that this is a bug. I reported it here.

ArrayAdapter : Remove by Index

Handle the collection of strings yourself with a List and pass the object into the constructor of the ArrayAdapter. This leaves you with a reference to the List so you can alter the data while allowing the adapter to manage and display as needed.

Note: When modifying the data object you must call

myAdapter.notifyDataSetChanged()

afterwards - which must also be on the UI thread. Obviously the changes to the list don't have to take place on the UI thread and should most likely not happen on the UI thread.

private ArrayList<String> mData = new ArrayList<String>();
private ArrayAdapter<String> mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
// Code that adds the strings
// Create the list adapter
mAdapter = new ArrayAdapter<String>(myActivity.this, android.R.layout.simple_list_item_1, mData);
}

private void removeItem(int index) {
mData.removeAt(index);
myActivity.this.runOnUiThread(new Runnable() {
public void run() {
mAdapter.notifyDataSetChanged();
}
}
}


Related Topics



Leave a reply



Submit