两次删除项目时发生java.lang.IndexOutOfBoundsException

我有一个适配器列表视图,当我点击删除功能(holder.image)显示此erorr。 这里是我的适配器:

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;
            }

并在日志中的情况是:

E / AndroidRuntime:致命异常:主要过程:japfa.co.id.assetmobile,PID:21794 java.lang.IndexOutOfBoundsException:无效索引4,大小为4在java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)在java.util.ArrayList.get(ArrayList.java:308)在japfa.co.id.mobilesales.adapter.AdapterChekstokList $ 6.onTextChanged(AdapterChekstokList.java:250)在android.widget.TextView.sendOnTextChanged(TextView.java:8951 )在android.widget.TextView.setText(TextView.java:4850)在android.widget.TextView.setText(TextView.java:4671)在android.widget.EditText.setText(EditText.java:101)在android.widget .TextView.setText(TextView.java:4646)at android.widget.AbsListView.obtainView(AbsListView.java:2823)at japfa.co.id.mobilesales.adapter.AdapterChekstokList.getView(AdapterChekstokList.java:116)。 widget.ListView.makeAndAddView(ListView.java:1884)在android.widget.ListView.fillUp(ListView.java:749)android.widget.ListView.correctTooHigh(ListView.java:1441)在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)在android.view.ViewGroup.layout(ViewGroup.java:5328)在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)在机器人。 widget.LinearLayout.onLayout(LinearLayout.java:1465)在android.view.View.layout(View.java:16695)在android.view.ViewGroup.layout(ViewGroup.java:5328)android.support.design.widget .CoordinatorLayout.layoutChild(CoordinatorLayout.java:1167)在android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:852)在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 )在android.view.View.layout(View.java:16695)android.view.ViewGroup.layout(ViewGroup.java:5328)在android.support.android.widget.FrameLayout.onLayout(FrameLayout.java:508) .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。在android.view.View.Group.layout(ViewGroup)的android.view.View.layout(View.java:16695)android.widget.FrameLayout.onLayout(FrameLayout.java:508)FrameLayout.layoutChildren(FrameLayout.java:573)的.java:5328)在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)在android.widget.LinearLayout.onLayout(LinearLayout.java:1465)在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)在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1191)在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6642)在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。 android.os.Handler.handleCallback(Handler.java:739)在android.os.Handler.dispatchMessage(Handler.java:95)android.os.Looper.loop(Looper.java:java:763) 145)在androi


我的问题解决了。 我编辑了代码:

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

holder.txexpired.getText().toString()

on public void onTextChanged。

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

上一篇: java.lang.IndexOutOfBoundsException when delete item twice

下一篇: volley NullPointerException