In my App I have two lists of vehicles: full list and my favorites. When In favorites I show the option for the user to delete the vehicle from this favorites:
To show this custom listview item I implemented the following adapter:
public class VehicleAdapter extends ArrayAdapter<Vehicle> {
private int listType=1;
public Vehicle[] vehicles;
public VehicleAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull 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);
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);
final int itemPosition = position;
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Vehicle item = VehicleAdapter.this.vehicles[itemPosition];
remove(item); // HERE: throws fatal error
notifyDataSetChanged();
}
});
}
return customView;
}
}
How I instantiate the adapter in the fragment (I'm loading the list inside a fragment):
Vehicle[] items = new Vehicle[vehicles.size()];
items = vehicles.toArray(items);
ListAdapter vehicleAdapter = new VehicleAdapter(getActivity(), R.layout.vehicle_row, items, mListType);
final ListView listView = (ListView) getView().findViewById(R.id.listView);
listView.setAdapter(vehicleAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Vehicle vehicle = (Vehicle) adapterView.getItemAtPosition(i);
((MainActivity)getActivity()).loadVehicleInformation(vehicle);
}
});
The exception:
Exception java.lang.UnsupportedOperationException:
java.util.AbstractList.remove (AbstractList.java:638)
java.util.AbstractList$SimpleListIterator.remove (AbstractList.java:75)
java.util.AbstractCollection.remove (AbstractCollection.java:229)
android.widget.ArrayAdapter.remove (ArrayAdapter.java:244)
xx.xx.xxxxxx.VehicleAdapter.remove (VehicleAdapter.java:78)
xx.xx.xxxx.VehicleAdapter$1.onClick (VehicleAdapter.java:62)
android.view.View.performClick (View.java:4756)
android.view.View$PerformClick.run (View.java:19749)
android.os.Handler.handleCallback (Handler.java:739)
android.os.Handler.dispatchMessage (Handler.java:95)
android.os.Looper.loop (Looper.java:135)
android.app.ActivityThread.main (ActivityThread.java:5221)
My problem: I'm not sure if I'm using the right approach with the delete button. As you can see I add a listener to it in the adapter class. I can get the right item but when I try to remove it throws a fatal error. I believe I'm using the wrong approach but I'm lost on how I could do this delete option.
Thanks for any help
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.