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

时间:2017-06-14 17:09:42

标签: android android-arrayadapter

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

要显示此自定义列表视图项,我实现了以下适配器:

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)

我的问题:我不确定我是否使用了删除按钮的正确方法。如您所见,我在适配器类中添加了一个侦听器。我可以得到正确的项目,但当我尝试删除它会引发致命的错误。我相信我使用了错误的方法,但我对如何删除这个选项感到很遗憾。

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

而不是发送,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;
    }
}