我有一個適配器列表視圖,當我點擊刪除功能(holder.image)顯示此erorr。這裏是我的適配器:java.lang.IndexOutOfBoundsException當刪除項目兩次
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 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at 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) 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) 在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) 在android.view.ViewGroup.layout(ViewGroup.java:5328) 在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077) at android.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) 在 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) 在機器人。 view.View.layout(查看。的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) 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.Linea在android.view.View中,android.widget.LinearLayout.onLayout(LinearLayout.java:1465) 上的android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556) 。佈局(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在Android.view.android.view.Choreographer.doCallbacks(Choreographer.java:590) 。 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
不要轉儲你的代碼,發佈相關的代碼就夠了。 –
可能重複[Android:IndexOutOfBounds錯誤時刪除列ListView中的行](https://stackoverflow.com/questions/12058775/android-indexoutofbounds-error-when-deleting-row-in-listview) –
@ModularSynth當我刪除該項目兩次在不同的項目,第一次刪除成功,但接下來我刪除該項目總是出錯。那麼,我的錯在哪裏? – user2901572