![](/img/trans.png)
[英]Android: how to remove an item from a listView and arrayAdapter
[英]Android ArrayAdapter: how to delete an item from within the 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;
}
}
我如何實例化片段中的適配器(我將列表加載到片段中):
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);
}
});
例外:
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)
我的問題:我不確定是否使用正確的方法使用刪除按鈕。 如您所見,我在適配器類中為其添加了一個偵聽器。 我可以獲得正確的商品,但是當我嘗試將其刪除時會引發致命錯誤。 我相信我使用了錯誤的方法,但是我對如何執行此刪除選項迷失了。
謝謝你的幫助
Verhicle []而不是發送,而是以ArrayList的形式發送,如果我們以數組形式發送,則無法在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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.