ArrayIndexOutOfBoundException删除单个记录

时间:2018-06-11 17:59:21

标签: android sqlite android-sqlite sqliteopenhelper android-sql

我正在使用带有预填充数据的外部数据库。用户还可以在该数据库中执行CRUD操作。但是当我删除记录时,它会将Array抛出绑定异常。这是我用来删除记录的代码片段。

String part[] = ((ItemModel) parent.getItemAtPosition(position)).getName().split("-");
String childQuery = "delete from Stock where item_id = (select id from Item where name = '" + part[0].trim() + "' and size = " + part[1].trim() + ")";
DBHelper.execute(childQuery);
String deleteQuery = "delete from Item where name  = '" + part[0].trim() + "' and size = " + part[1].trim() + " ";
DBHelper.execute(deleteQuery);
customAdapter.notifyDataSetChanged();
dismiss();

try {
    if (!TextUtils.isEmpty(InOutStockActivity.etInOutName.getText().toString())) {
        InOutStockActivity.etInOutName.setText("");
    }

    if (!TextUtils.isEmpty(TotalStockActivity.etTotalName.getText().toString())) {
        TotalStockActivity.etTotalName.setText("");
    }
} catch (NullPointerException e) {
    e.printStackTrace();
}
TastyToast.makeText(mContext, "Record Deleted Successfully.", TastyToast.LENGTH_LONG, TastyToast.DEFAULT);

这里是我的logcat中的错误。

java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at com.v.m.fragment.NameFragment$2$1.onClick(NameFragment.java:102)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:162)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

提前致谢。

1 个答案:

答案 0 :(得分:2)

name不包含“ - ”,但您的代码假定为。