Android ArrayAdapter:如何从适配器中删除项目

Android ArrayAdapter:如何从适配器中删除项目,android,android-arrayadapter,Android,Android Arrayadapter,在我的应用程序中,我有两个车辆列表:完整列表和我的收藏夹。在收藏夹中时,我会显示用户从该收藏夹中删除车辆的选项: 为了显示此自定义listview项,我实现了以下适配器: public class VehicleAdapter extends ArrayAdapter<Vehicle> { private int listType=1; public Vehicle[] vehicles; public VehicleAdapter(@NonNull

在我的应用程序中,我有两个车辆列表:完整列表和我的收藏夹。在收藏夹中时,我会显示用户从该收藏夹中删除车辆的选项:

为了显示此自定义listview项,我实现了以下适配器:

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;
    }
}
我的问题是:我不确定我是否使用了删除按钮的正确方法。如您所见,我在adapter类中向其添加了一个侦听器。我可以得到正确的项目,但当我试图删除它抛出一个致命的错误。我相信我使用了错误的方法,但我不知道如何做这个删除选项


感谢您提供的帮助

如果我们以数组形式发送,则无法在ArrayAdapter中修改它,而不是发送,请选择Verhicle[]以ArrayList形式发送

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;
    }
}
public class VehicleAdapter扩展阵列适配器{
私有int-listType=1;
公共列表车辆;
公共车辆导航器(@NonNull上下文上下文、@LayoutRes int-resource、@NonNull ArrayList对象、int-listType){
超级(上下文、资源、对象);
this.listType=listType;
这意味着车辆=物体;
}
@非空
@凌驾
公共视图getView(int位置,@Nullable视图convertView,@NonNull视图组父级){
LayoutInflater LayoutInflater=LayoutInflater.from(getContext());
视图customView=布局更平坦。充气(R.layout.vehicle\u row,空);
最终车辆=获取项目(位置);
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(车辆名称);
txtDescription.setText(车辆简短描述);
毕加索.with(getContext()).load(vehicle.picture).into(imgVehicle);
//如果是收藏夹列表
如果(listType==2){
按钮btnDelete=(按钮)customView.findViewById(R.id.btnDelete);
btnDelete.setVisibility(View.VISIBLE);
btnDelete.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
移走(车辆);
notifyDataSetChanged();
}
});
}
返回自定义视图;
}
}

不是发送,而是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;
    }
}
public class VehicleAdapter扩展阵列适配器{
私有int-listType=1;
公共列表车辆;
公共车辆导航器(@NonNull上下文上下文、@LayoutRes int-resource、@NonNull ArrayList对象、int-listType){
超级(上下文、资源、对象);
this.listType=listType;
这意味着车辆=物体;
}
@非空
@凌驾
公共视图getView(int位置,@Nullable视图convertView,@NonNull视图组父级){
LayoutInflater LayoutInflater=LayoutInflater.from(getContext());
视图customView=布局更平坦。充气(R.layout.vehicle\u row,空);
最终车辆=获取项目(位置);
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(车辆名称);
txtDescription.setText(车辆简短描述);
毕加索.with(getContext()).load(vehicle.picture).into(imgVehicle);
//如果是收藏夹列表
如果(listType==2){
按钮btnDelete=(按钮)customView.findViewById(R.id.btnDelete);
btnDelete.setVisibility(View.VISIBLE);
btnDelete.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
移走(车辆);
notifyDataSetChanged();
}
});
}
返回自定义视图;
}
}

请同时发布logcat错误和你的remove()方法。如果你在你的应用程序中展示了实例化适配器的几行代码,这将非常有用。这个问题可能与适配器的实例化方式有关,当您试图修改实例化适配器的实时内容时,会在运行时出现异常。@Livio我刚才在问题中添加了一个问题,您应该更改代码,因为无法修改Vehicle[]项,所以您应该使用类似ArrayList项的内容,这是可修改的,不会生成问题所在的ExceptionTanks@Livio。还请发布logcat错误和您的remove()方法。如果您在应用程序中显示实例化适配器的几行代码,这将非常有用。这个问题可能与适配器的实例化方式有关,当您试图修改实例化适配器的实时内容时,会在运行时出现异常。@Livio我刚才在问题中添加了一个问题,您应该更改代码,因为无法修改Vehicle[]项,所以您应该使用类似ArrayList项的内容,这是可修改的,不会生成问题所在的ExceptionTanks@Livio。感谢您的回答。它给了我同样的错误:class java.lang.UnsupportedOperationException更新了我的答案,看一看。就是这样。ArrayList是对象的正确类型。谢谢你的回答。它给了我同样的错误:class java.lang.UnsupportedOperationException更新了我的答案,看一看。就是这样。ArrayList是对象的正确类型。谢谢
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;
    }
}