java.lang.IndexOutOfBoundsException:索引6无效,列表

时间:2016-07-20 07:14:46

标签: android indexoutofboundsexception

当我尝试删除任何有IndexoutofBound错误错误的内容时,有删除的imageview。

我无法从其他答案中解决。

在setText方法中生成异常。

private List<String> ids = new ArrayList<>();
private List<String> names = new ArrayList<>();
private List<String> vehicleno = new ArrayList<>();
private List<String> types = new ArrayList<>();
private Activity context;
private Context cxt;
private String vehicleText;
private static MyClickListener myClickListener;


public CustomList(Activity context, String[] ids, String[] types, String[] names, String[] vehicleno) {
    super(context, R.layout.list_item, ids);
    this.ids.addAll(Arrays.asList(ids));
    this.types.addAll(Arrays.asList(types));
    this.context = context;
    this.names.addAll(Arrays.asList(names));
    this.vehicleno.addAll(Arrays.asList(vehicleno));
}

public void exitListItem(final int position){
    StringRequest stringRequest = new StringRequest(Request.Method.POST, EXIT_VISITOR_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    ids.remove(position);
                    types.remove(position);
                    names.remove(position);
                    vehicleno.remove(position);
                    notifyDataSetChanged();
                    Toast.makeText(context,"Exit Successfully",Toast.LENGTH_LONG).show();
                    Log.i("Logg",response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.i("Log",error.toString());
                    if (error.networkResponse == null) {
                        if (error.getClass().equals(TimeoutError.class)) {
                            // Show timeout error message
                            Toast.makeText(getContext(), "Oops. Timeout error!", Toast.LENGTH_LONG).show();
                        }
                    }
                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put(KEY_TYPE_OF_VISITOR, textType);
            params.put(KEY_VEHICLE_NUMBER,textVehicleNo);
            return params;
        }
    };
    stringRequest.setRetryPolicy(new DefaultRetryPolicy(
            5000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    RequestQueue requestQueue = Volley.newRequestQueue(context);
    requestQueue.add(stringRequest);

}

@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
    View view = convertView;
    Holder holder = null;
    if (view == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        view = inflater.inflate(R.layout.list_item, parent, false);

        holder = new Holder();
        holder.txtName = (TextView) view.findViewById(R.id.txt_exit_name);
        holder.txtType = (TextView) view.findViewById(R.id.txt_exit_type);
        holder.txtVehicleNo = (TextView) view.findViewById(R.id.txt_exit_vehicle_num);
        holder.imgBtnImg = (ImageView) view.findViewById(R.id.img_btn_exit);
        vehicleText = holder.txtVehicleNo.getText().toString();
        holder.imgBtnImg.setFocusable(false);
        view.setTag(holder);
    } else {
        holder = (Holder) view.getTag();
    }
    holder.txtName.setText(names.get(position));
    holder.txtType.setText(types.get(position));
    holder.txtVehicleNo.setText(vehicleno.get(position));
    holder.imgBtnImg.setImageResource(R.drawable.exit);
    holder.imgBtnImg.setFocusable(false);

    holder.imgBtnImg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            textType = types.get(position);
            textVehicleNo=vehicleno.get(position);
            exitListItem(position);

            //Toast.makeText(context,textType,Toast.LENGTH_LONG).show();
            //Toast.makeText(context,textVehicleNo,Toast.LENGTH_LONG).show();

        }
    });
    notifyDataSetChanged();

    return view;
}

public interface MyClickListener {
    void onClick(int position, View v);
}

public void setOnClickListener(MyClickListener myClickListener) {
    this.myClickListener = myClickListener;
}


private class Holder {

    TextView txtid;
    TextView txtName;
    TextView txtVehicleNo;
    TextView txtType;
    ImageView imgBtnImg;
}

当我试图删除任何我发现indexoutofbound错误 请解决它

日志

  

java.lang.IndexOutOfBoundsException:索引6无效,大小为6                                                                                在   java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)                                                                                at java.util.ArrayList.get(ArrayList.java:304)                                                                                在com.example.lionden.lionden.CustomList.getView(CustomList.java:146)                                                                                在android.widget.AbsListView.obtainView(AbsListView.java:2319)                                                                                在android.widget.ListView.measureHeightOfChildren(ListView.java:1250)                                                                                在android.widget.ListView.onMeasure(ListView.java:1162)                                                                                在android.view.View.measure(View.java:15264)

1 个答案:

答案 0 :(得分:-2)

如果列表大小为6,则索引应为0到5。