I'm a novice Android developer, using Android Studio and I'm attempting to develop my first app. I would like to remove an item from a list if it is clicked, but I'm getting the following exceptions/errors:
Caused by: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
at android.widget.AdapterView.setOnClickListener(AdapterView.java:783)
When I click on the link "AdapterView.java:783" Android studio takes me to this following interface:
public interface OnItemClickListener {
void onItemClick(AdapterView<?> var1, View var2, int var3, long var4);
}
The code I'm trying to implement is:
public void removeItem(View view){
/*
Sample:
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
*/
ListView vehiclesLst = (ListView) findViewById(R.id.vehicle_list);
ArrayAdapter<String> vehicleAdapter =
new ArrayAdapter<String>(this, R.layout.activity_vehiclelist, vehicleList);
vehiclesLst.setAdapter(vehicleAdapter);
vehiclesLst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String vehicleItem = ((TextView) view).getText().toString();
vehicleList.remove(vehicleItem);
}
});
}
I don't understand where the code is finding the setOnClickListener method. Does anyone know of any documentation that explains what is happening?
Why this?:
String vehicleItem = ((TextView) view).getText().toString(); vehicleList.remove(vehicleItem);
You just:
VehicleList.remove(i);
Remove() method parameter would be the position of the array as an int and you are trying to pass a string.
More info: Link
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.