-1

J'ai un adaptateur listview, lorsque je clique sur la fonction de suppression (holder.image) montre cette erorr. Et voici mon adaptateur:java.lang.IndexOutOfBoundsException lors de la suppression de l'élément deux fois

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

et le journal en cas est:

E/AndroidRuntime: FATAL EXCEPTION: principale processus: japfa.co.id.assetmobile, PID: 21794 java.lang.IndexOutOfBoundsException: index non valide 4, la taille est de 4 à java.util.ArrayList.throwIndexOutOfBoundsException (ArrayList.java:255) à java.util.ArrayList.get (ArrayList.java:308) au japfa.co.id.mobilesales.adapter.AdapterChekstokList $ 6.onTextChanged (AdapterChekstokList.java:250) à android.widget.TextView.sendOnTextChanged (TextView.java:8951) at 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) au japfa.co.id.mobilesales.adapter.AdapterChekstokList.getView (AdapterChekstokList.java:116) at android.widget.AbsListView.obtainView (AbsListView.java:2823) à android.widget.ListView.makeAndAddView (ListView.java:1884) à android.widget.ListView.fillUp (ListView.java:749) à android.widget.ListView.correctTooHigh (ListView.java:1441) à androïde .widget.ListView.fillSpecific (ListView.java:1379) à android.widget.ListView.layoutChildren (ListView.java:1671) à android.widget.AbsListView.onLayout (AbsListView.java:2627) à android.view .View.layout (View.java:16695) à android.view.ViewGroup.layout (ViewGroup.java:5328) à android.widget.RelativeLayout.onLayout (RelativeLayout.java:1077) au android.view.View.layout (View.java:16695) à android.view.ViewGroup.layout (ViewGroup.java:5328) à android.widget.LinearLayout.setChildFrame (LinearLayout.java:1702) at android .widget.LinearLayout.layoutVertical (LinearLayout.java:1556) at android.widget.LinearLayout.onLayout (LinearLayout.java:1465) à l'adresse android.view.View.layout (View.java:16695) at android.view .ViewGroup.layout (ViewGroup.java:5328) au android.support.design.widget.CoordinatorLayout.layoutChild (CoordinatorLayout.java:1167) au android.support.design.widget .CoordinatorLayout.onLayoutChild (CoordinatorLayout.java:852) à android.support.design.widget.CoordinatorLayout.onLayout (CoordinatorLayout.java:871) à android.view.View.layout (View.java:16695) à android.view.ViewGroup.layout (ViewGroup.java:5328) à android.widget.FrameLayout.layoutChildren (FrameLayout.java:573) à android.widget.FrameLayout.onLayout (FrameLayout.java:508) à android. view.View.layout (Voir.java: 16695) à android.view.ViewGroup.layout (ViewGroup.java:5328) à android.support.v7.widget.ActionBarOverlayLayout.onLayout (ActionBarOverlayLayout.java:437) à android.view.View.layout (View.java:16695) à android.view.ViewGroup.layout (ViewGroup.java:5328) à android.widget.FrameLayout.layoutChildren (FrameLayout.java:573) à android.widget.FrameLayout.onLayout (FrameLayout .java: 508) à android.view.View.layout (View.java:16695) à android.view.ViewGroup.layout (ViewGroup.java:5328) à android.widget.Linea rLayout.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) à android.view.ViewGroup.layout (ViewGroup.java:5328) à android.widget.FrameLayout.layoutChildren (FrameLayout.java:573) à android.widget.FrameLayout.onLayout (FrameLayout.java:508) à android.view.View.layout (View.java:16695) à android.view.ViewGroup.layout (ViewGroup.java:5328) à android.view.ViewRootImpl.perfor mLayout (ViewRootImpl.java:2319) à 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) à android.view.Choreographer.doCallbacks (Choreographer.java:590) à android.view. Choreographer.doFrame (Choreographer.java:560) au android.view.Choreographer $ FrameDisplayEventReceiver.run (Choreographer.java:763) à android.os.Handler.handleCallback (Handler.java:739) à android.os.Handler.dispatchMessage (Handler.java:95) à android.os.Looper.loop (Looper.java:145) à androi

+2

Ne pas vider votre code, afficher suffisamment de code pertinent. –

+0

Copie possible de [Android: IndexOutOfBounds Erreur lors de la suppression de ligne dans ListView] (https://stackoverflow.com/questions/12058775/android-indexoutofbounds-error-when-deleting-row-in-listview) –

+0

@ModularSynth lorsque j'ai supprimé l'article deux fois dans l'élément de différence, premier succès supprimé, mais ensuite j'ai supprimé l'élément toujours obtenir une erreur. Alors, où est ma faute? – user2901572

Répondre

0

mon problème a été résolu. i le code édité:

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

à

holder.txexpired.getText().toString() 

public void sur OnTextChanged.