【问题标题】:Last item in ListView returns OutOfBoundsExceptionListView 中的最后一项返回 OutOfBoundsException
【发布时间】:2017-07-12 07:48:58
【问题描述】:

我有一个旧应用程序,其中列表视图中的每个项目中还有一个删除按钮。我在最近的应用程序中使用了它的格式,但每当我尝试删除列表视图中的最后一项时,它都会给我OutOfBoundsException: Invalid index 4, size 4

错误发生在:

  • 我尝试删除列表中的最后一个
    • 我尝试删除列表中的only

这是我从列表中删除该项目的方法:

Cart_Adapter:

   productHolder.item_delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            cart_list.remove(getItem(position));
            notifyDataSetChanged();

我尝试了但都失败了:

  • remove(getItem(position)); 有和没有 notifyDataSetChanged
  • remove(cart_list.get(position));有和没有 notifyDataSetChanged
  • cart_list.remove(getItem(position));
  • cart_list.remove(position);

编辑: 全适配器类

package dagger.com.japorms.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import dagger.com.japorms.R;

import dagger.com.japorms.fragment.Cart_Fragment;
import dagger.com.japorms.main.User_Activity;
import dagger.com.japorms.model.Cart_List;
import dagger.com.japorms.model.Cart_Model;
import dagger.com.japorms.model.Order;
import dagger.com.japorms.model.Pending_Items_Model;
import dagger.com.japorms.model.Product_List;
import dagger.com.japorms.other.HttpConstants;
import dagger.com.japorms.service.APIService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static dagger.com.japorms.other.HttpConstants.BASE_URL;
import static dagger.com.japorms.other.HttpConstants.HTTP_PRODUCT_IMAGE;

/**
 * Created by Claude on 06/07/2017.
 */

public class Cart_Adapter extends ArrayAdapter<Cart_List> {

    Context context;
    int layoutResourceId;
    ArrayList<Cart_List> cart_list = new ArrayList<>();
    DecimalFormat formatter = new DecimalFormat("#,###,###.00");

    int item_list;
    int temp;

    Double total_amount =0.00d;

    public Cart_Adapter(Context context, int layoutResourceId, ArrayList<Cart_List> cart_list) {

        super(context,layoutResourceId,cart_list);

        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.cart_list = cart_list;
    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View row = convertView;

        ProductHolder productHolder;

        if(row == null) {
            LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
            row = inflater.inflate(layoutResourceId,parent,false);

            productHolder = new ProductHolder();
            productHolder.item_img = (ImageView) row.findViewById(R.id.item_cart_img);
            productHolder.item_name = (TextView) row.findViewById(R.id.item_cart_name);
            productHolder.item_price = (TextView) row.findViewById(R.id.item_cart_price);
            productHolder.item_qty = (TextView) row.findViewById(R.id.item_cart_qty);
            productHolder.item_size = (TextView) row.findViewById(R.id.item_cart_size);
            productHolder.item_modify = (TextView) row.findViewById(R.id.item_cart_modify);
            productHolder.item_delete = (TextView) row.findViewById(R.id.item_cart_remove);


            row.setTag(productHolder);


        }
        else

            productHolder = (ProductHolder) row.getTag();


            productHolder.item_name.setText("Name: "+cart_list.get(position).getItemName());
            productHolder.item_price.setText("Price: PHP " + formatter.format(Double.parseDouble(cart_list.get(position).getItemPrice())));
            productHolder.item_qty.setText("Quantity: "+cart_list.get(position).getItemQty());
            productHolder.item_size.setText("Size: "+cart_list.get(position).getSize());

            Picasso.with(context).load(BASE_URL + HTTP_PRODUCT_IMAGE + cart_list.get(position).getCategory() + "/" +
                    cart_list.get(position).getItemId() + ".jpg").into(productHolder.item_img);





        productHolder.item_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                cart_list.remove(getItem(position));
        notifyDataSetChanged();

                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(HttpConstants.BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();

                APIService service = retrofit.create(APIService.class);


                Call<Pending_Items_Model> call = service.deletePendingItem( cart_list.get(position).getItemId());

                call.enqueue(new Callback<Pending_Items_Model>() {
                    @Override
                    public void onResponse(Call<Pending_Items_Model> call, Response<Pending_Items_Model> response) {
                        ((User_Activity) context).CheckCart();
                    }

                    @Override
                    public void onFailure(Call<Pending_Items_Model> call, Throwable t) {

                    }
                });

            }
        });


        return row;



    }





    static class ProductHolder{

        ImageView item_img;
        TextView item_name;
        TextView item_price;
        TextView item_qty;
        TextView item_size;
        TextView item_modify;
        TextView item_delete;
    }
}

【问题讨论】:

  • 你的位置(不管它是什么)是1,所以如果你只有一项,位置是1,但你的列表只有索引0....使用position-1。如果这是您在列表视图中的位置,请记住您通过滚动获得了偏移量。
  • 你从哪里得到位置变量的值?
  • 你能说明一个条件吗?我还想检查 size() 和 get(position) 是否相等,然后我将删除位置 -1?
  • @AbdulWaheed 我将编辑我的问题并包含完整的适配器。
  • 不仅是最后一个,在任何情况下你都会有一个错误。

标签: android listview


【解决方案1】:

我认为问题是由以下两行的使用引起的: cart_list.remove(getItem(position)); ... Call<Pending_Items_Model> call = service.deletePendingItem(cart_list.get(position).getItemId());

通知 set 将无济于事,因为您尝试使用相同的方法将项目放在刚刚删除的位置上。如果您想获得该项目,我建议您重构:

Cart_List removedCartcart_list.remove(getItem(position)); ... Call<Pending_Items_Model> call = service.deletePendingItem(removedCart.getItemId());

【讨论】:

    猜你喜欢
    • 2021-07-30
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多