简体   繁体   English

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

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

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. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM