java.lang.IndexOutOfBoundsException when delete item twice

I have an adapter listview, when i click delete function(holder.image) show this erorr. And here is my adapter:

public View getView(final int position, View view, ViewGroup parent) {
                final ViewHolder holder;
                if (view == null) {
                    holder = new ViewHolder();
                    view = inflater.inflate(R.layout.list_adapter_checkstok, parent, false);holder.txproduk = (TextView) view.findViewById(R.id.txproduk);
                    holder.txqty = (TextView) view.findViewById(R.id.txqty);
                    holder.txexpired = (TextView) view.findViewById(R.id.txexpired);
                    holder.txidproduk = (TextView) view.findViewById(R.id.txidproduk);
                    holder.spin = (Spinner) view.findViewById(R.id.spin);
        holder.imgexpired = (ImageView) view.findViewById(R.id.imgexpired);
                    holder.image = (Button) view.findViewById(R.id.image);
                    holder.image2 = (ImageView) view.findViewById(R.id.image2);
                    holder.image2.setVisibility(View.INVISIBLE);
        Database = new SQLite(context);
                    db = Database.getReadableDatabase();
        view.setTag(holder);
                } else {
                    holder = (ViewHolder) view.getTag();
                }
                tipe=ListViewCheckStock.get(position).getjnsprod();
                idprod=ListViewCheckStock.get(position).getidprod();
                unit=ListViewCheckStock.get(position).getunit();
                if (ListViewCheckStock.get(position).gettxproduk().equals("")) {
                    holder.txproduk.setText("");
                } else {
                    holder.txproduk.setText(ListViewCheckStock.get(position).gettxproduk());
                }
                if (ListViewCheckStock.get(position).getjnsprod().equals("")) {
                    holder.txidproduk.setText("");
                } else {
                    holder.txidproduk.setText("Produk " + ListViewCheckStock.get(position).getjnsprod());
                }
                if (ListViewCheckStock.get(position).gettxqty().equals("")) {
                    holder.txqty.setText("");
                } else {
                    holder.txqty.setText(ListViewCheckStock.get(position).gettxqty());
                }
                if (ListViewCheckStock.get(position).getexpired().equals("")) {
                    holder.txexpired.setText("");
                } else {
                    holder.txexpired.setText(ListViewCheckStock.get(position).getexpired());
                }
        final DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int yr, int monthOfYear,
                                          int dayOfMonth) {
                        // TODO Auto-generated method stub
                        year = yr;
                        month = monthOfYear;
                        day = dayOfMonth;
                        holder.txexpired.setText(new StringBuilder().append(day).append("/")
                                .append(month + 1).append("/").append(year));
        holder.image2.setVisibility(View.VISIBLE);
                    }
                }; holder.imgexpired.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
         DatePickerDialog();
                        myCalendar = Calendar.getInstance();
                        dialog = new DatePickerDialog(context,
                                dateListener, year, month, day);
                        dialog.show();
         }
                });
                holder.image.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
        android.app.AlertDialog.Builder dialog = new android.app.AlertDialog.Builder(
                                context);
                        dialog.setMessage("Anda Yakin Ingin Menghapus Data Ini ?");
                        dialog.setPositiveButton("OK",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        String deleted;
                                        deleted = String.valueOf((ListViewCheckStock.get(position).getidtable()));
                                        System.out.println("Deleteeeeee" + deleted);
                                        db = Database.getWritableDatabase();
                                        db.delete("TBL_CHECKSTOCK", "TBL_CHECKSTOCK._id=?", new String[]{deleted});
        ListViewCheckStock.remove(position);
                                        notifyDataSetChanged();
                                    }
                                });
                        dialog.setNegativeButton("Kembali",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) { dialog.dismiss();
                                    }
                                });
                        dialog.show();
                    }
                });
        holder.image2.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
        android.app.AlertDialog.Builder dialog = new android.app.AlertDialog.Builder(
                                context);
                        dialog.setMessage("Anda Yakin Ingin Menyimpan Perubahan Data Ini ?");
                        dialog.setPositiveButton("OK",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {dialog.dismiss();
                                        String saving;
                                        saving = String.valueOf((ListViewCheckStock.get(position).getidtable()));
                                        System.out.println("saving" + saving);
        db = Database.getWritableDatabase();
        Database.updateSavingCheckstok(ListViewCheckStock.get(position).getunit(), saving, ListViewCheckStock.get(position).gettxqty(), ListViewCheckStock.get(position).getexpired());
         holder.txqty.clearFocus();
        holder.image2.setVisibility(View.INVISIBLE);
                                    }
                                });
                        dialog.setNegativeButton("Kembali",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                });
                        dialog.show();
         }
                });
                holder.txqty.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (!hasFocus) {
                            holder.image2.setVisibility(View.VISIBLE);
                        }
                    }
                });
                holder.txqty.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }
         @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if ((holder.txqty.getText().toString().contains(".") && !holder.spin.getSelectedItem().toString().equals("CAR")) &&
                                (holder.txqty.getText().toString().contains(".") && !holder.spin.getSelectedItem().toString().equals("KG"))) {
                            CustomDialog.init.setDialog(context,
                                    "Format angka", "Desimal tidak diperbolehkan dalam unit ini.",
                                    "ok", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            holder.txqty.setText("");
                                        }
                                    });
                        }
                        Model_CheckStock itempos = ListViewCheckStock.get(position);
                        itempos.settxqty(holder.txqty.getText().toString());
                        Log.w("TAG >>", "onTextChanged : " + itempos.gettxqty());
        }
        @Override
                    public void afterTextChanged(Editable s) {
                    }
                });
                holder.txexpired.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }
         @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        Model_CheckStock itempos = ListViewCheckStock.get(position);
                        itempos.setexpired(holder.txexpired.getText().toString());
                        Log.w("TAG >>", "onTextChanged expired : " + itempos.getexpired());
                    }
         @Override
                    public void afterTextChanged(Editable s) {
                    }
                });
        holder.spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
         @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view,
                                               int arg2, long id) {
         if ((holder.txqty.getText().toString().contains(".") && !holder.spin.getSelectedItem().toString().equals("CAR")) &&
                                (holder.txqty.getText().toString().contains(".") && !holder.spin.getSelectedItem().toString().equals("KG"))) {
                            CustomDialog.init.setDialog(context,
                                    "Format angka", "Desimal tidak diperbolehkan dalam unit ini.",
                                    "ok", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            holder.txqty.setText("");
                                        }
                                    });
                        }
                        ListViewCheckStock.get(position).setunit(adapterView.getSelectedItem().toString());
                        Log.w("TAG >>", "onItemSelected: " + ListViewCheckStock.get(position).getunit());
                        holder.spin.setSelection(arg2);
                    }
        @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {
                        // TODO Auto-generated method stub
         }
                });
                holder.spin.setOnTouchListener(new View.OnTouchListener(){
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        holder.image2.setVisibility(View.VISIBLE);
                        return false;
                    }
         });
                if (ListViewCheckStock.get(position).getidprod().contains("NONSGF")) {
                    List<Model_Unit> chekstok = Database.getUnitAll();
                    ArrayAdapter<Model_Unit> dataAdapter = new ArrayAdapter<Model_Unit>(
                            context, android.R.layout.simple_spinner_item, chekstok);
                    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    holder.spin.setAdapter(dataAdapter);
                    holder.spin.setSelection(getIndex(holder.spin, ListViewCheckStock.get(position).getunit()));
                } else {
                    List<Model_Unit> chekstok = Database.getUnit(ListViewCheckStock.get(position).getidprod());
                    ArrayAdapter<Model_Unit> dataAdapter = new ArrayAdapter<Model_Unit>(
                            context, android.R.layout.simple_spinner_item, chekstok);
                    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    holder.spin.setAdapter(dataAdapter);
                    holder.spin.setSelection(getIndex(holder.spin, ListViewCheckStock.get(position).getunit()));
                }
        if (ListViewCheckStock.get(position).getflagstats().equals("0")) {
        holder.image.setVisibility(View.VISIBLE);
        //            holder.image2.setVisibility(View.GONE);
                } else {
                    holder.txqty.setEnabled(false);
                    holder.spin.setEnabled(false);
                    holder.txexpired.setEnabled(false);
                    holder.imgexpired.setVisibility(View.GONE);
                    holder.image.setVisibility(View.GONE);
                    holder.image2.setVisibility(View.GONE);
                    view.setBackgroundResource(R.color.grayLlight);
                }
                return view;
            }

and the log for in case is :

E/AndroidRuntime: FATAL EXCEPTION: main Process: japfa.co.id.assetmobile, PID: 21794 java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.get(ArrayList.java:308) at japfa.co.id.mobilesales.adapter.AdapterChekstokList$6.onTextChanged(AdapterChekstokList.java:250) at android.widget.TextView.sendOnTextChanged(TextView.java:8951) at android.widget.TextView.setText(TextView.java:4850) at android.widget.TextView.setText(TextView.java:4671) at android.widget.EditText.setText(EditText.java:101) at android.widget.TextView.setText(TextView.java:4646) at japfa.co.id.mobilesales.adapter.AdapterChekstokList.getView(AdapterChekstokList.java:116) at android.widget.AbsListView.obtainView(AbsListView.java:2823) at android.widget.ListView.makeAndAddView(ListView.java:1884) at android.widget.ListView.fillUp(ListView.java:749) at android.widget.ListView.correctTooHigh(ListView.java:1441) at android. widget.ListView.fillSpecific(ListView.java:1379) at android.widget.ListView.layoutChildren(ListView.java:1671) at android.widget.AbsListView.onLayout(AbsListView.java:2627) at android.view.View.layout(View.java:16695) at android.view.ViewGroup.layout(ViewGroup.java:5328) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077) at android.view.View.layout(View.java:16695) at android.view.ViewGroup.layout(ViewGroup.java:5328) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556) at android.widget.LinearLayout.onLayout(LinearLayout.java:1465) at android.view.View.layout(View.java:16695) at android.view.ViewGroup.layout(ViewGroup.java:5328) at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1167) at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:852) at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLay out.java:871) at android.view.View.layout(View.java:16695) at android.view.ViewGroup.layout(ViewGroup.java:5328) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) at android.widget.FrameLayout.onLayout(FrameLayout.java:508) at android.view.View.layout(View.java:16695) at android.view.ViewGroup.layout(ViewGroup.java:5328) at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437) at android.view.View.layout(View.java:16695) at android.view.ViewGroup.layout(ViewGroup.java:5328) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) at android.widget.FrameLayout.onLayout(FrameLayout.java:508) at android.view.View.layout(View.java:16695) at android.view.ViewGroup.layout(ViewGroup.java:5328) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556) at android.widget.LinearLayout.onLayout(LinearLayout.java:1465) at android.view.View.layout(View.java :16695) at android.view.ViewGroup.layout(ViewGroup.java:5328) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) at android.widget.FrameLayout.onLayout(FrameLayout.java:508) at android.view.View.layout(View.java:16695) at android.view.ViewGroup.layout(ViewGroup.java:5328) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2319) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2032) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1191) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6642) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777) at android.view.Choreographer.doCallbacks(Choreographer.java:590) at android.view.Choreographer.doFrame(Choreographer.java:560) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java: 145) at androi


my problem solved. i edited the code :

Model_CheckStock itempos = ListViewCheckStock.get(position);
                    itempos.setexpired(holder.txexpired.getText().toString());

to

holder.txexpired.getText().toString()

on public void onTextChanged.

链接地址: http://www.djcxy.com/p/16638.html

上一篇: 当在kitkat上设置minDate或maxDate时,Android DatePicker崩溃

下一篇: 两次删除项目时发生java.lang.IndexOutOfBoundsException