简体   繁体   English

Android sqlite数据库错误IndexOutOfBoundException

[英]Android sqlite database error IndexOutOfBoundException

I am trying to delete data from sq-lite database, data is loaded in custom list-view with check-box. 我正在尝试从sq-lite数据库中删除数据,数据已在带有复选框的自定义列表视图中加载。 When user check some check-box and then press delete button then it should delete all those data which is checked. 当用户选中某个复选框,然后按删除按钮时,它应删除所有已选中的数据。

Everything is working fine it is deleting all data which is checked. 一切工作正常,正在删除所有已检查的数据。 But the problem is occurring at last two data. 但是问题出在最后两个数据上。 When last 2 data is remaining, and if i select both data for deleting then it is giving error. 当剩下最后2个数据时,如果我选择两个数据都删除了,那么它给出了错误。

Logcat is below Logcat在下面

12-16 12:55:51.049: E/AndroidRuntime(11489): FATAL EXCEPTION: main
12-16 12:55:51.049: E/AndroidRuntime(11489): java.lang.IndexOutOfBoundsException:     Invalid index 1, size is 1
12-16 12:55:51.049: E/AndroidRuntime(11489):    at   java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
12-16 12:55:51.049: E/AndroidRuntime(11489):    at java.util.ArrayList.get(ArrayList.java:304)
12-16 12:55:51.049: E/AndroidRuntime(11489):    at iqualtech.skirr.Classes$1$1.onClick(Classes.java:98)
12-16 12:55:51.049: E/AndroidRuntime(11489):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:169)
12-16 12:55:51.049: E/AndroidRuntime(11489):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-16 12:55:51.049: E/AndroidRuntime(11489):    at   android.os.Looper.loop(Looper.java:154)
12-16 12:55:51.049: E/AndroidRuntime(11489):    at android.app.ActivityThread.main(ActivityThread.java:4624)
12-16 12:55:51.049: E/AndroidRuntime(11489):    at java.lang.reflect.Method.invokeNative(Native Method)
12-16 12:55:51.049: E/AndroidRuntime(11489):    at java.lang.reflect.Method.invoke(Method.java:511)
12-16 12:55:51.049: E/AndroidRuntime(11489):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
12-16 12:55:51.049: E/AndroidRuntime(11489):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
12-16 12:55:51.049: E/AndroidRuntime(11489):    at dalvik.system.NativeStart.main(Native Method)

and my delete function is as below 我的删除功能如下

private void deleteMenuSpinner() {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                    Classes.this);

            final Spinner spinnerDelete = new Spinner(Classes.this);
            alertDialog.setView(spinnerDelete);

            adapterSpinner = ArrayAdapter.createFromResource(Classes.this,
                    R.array.delete_menu,
                    android.R.layout.simple_spinner_item);
            adapterSpinner
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerDelete.setAdapter(adapterSpinner);

            alertDialog.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                            Code.i = true;
                            int len = mListView.getCount();
                            SparseBooleanArray checked = mListView
                                    .getCheckedItemPositions();
                            for (int i = 0; i < len; i++)
                                if (checked.get(i)) {
                                    Code.i = false;
                                    String[] delete = names2.get(i);
                                    String idString = delete[0];
                                    long idLong = Long.valueOf(idString);
                                    Log.d("Deleting...", idLong + "");
                                    dataManipulator.delete(idLong);
                                    names2.remove(i);
                                }
                            if (Code.i == true) {
                                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                                        Classes.this);
                                alertDialogBuilder.setTitle("No Data");
                                alertDialogBuilder
                                        .setMessage(
                                                "No Data Available to Delete")
                                        .setCancelable(false)
                                        .setPositiveButton(
                                                "Ok",
                                                new DialogInterface.OnClickListener() {
                                                    public void onClick(
                                                            DialogInterface dialog,
                                                            int id) {
                                                    }
                                                });
                                AlertDialog alertDialog = alertDialogBuilder
                                        .create();
                                alertDialog.show();
                            } else {
                                names2 = dataManipulator.selectAll();
                                stg1 = new String[names2.size()];
                                int x = 0;
                                String stg;

                                for (String[] name : names2) {
                                    stg = "Class Name : " + name[1];
                                    stg1[x] = stg;
                                    x++;
                                }
                                adapter = new ArrayAdapter<String>(
                                        Classes.this,
                                        R.layout.custom_list_item_multiple_choice,
                                        stg1);
                                mListView
                                        .setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                                mListView
                                        .setBackgroundResource(R.drawable.assignmentheader);
                                mListView
                                        .setCacheColorHint(Color.TRANSPARENT);
                                mListView.setAdapter(adapter);
                                adapter.notifyDataSetChanged();
                            }
                        }
                    });

            alertDialog.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                            dialog.cancel();
                        }
                    });
            alertDialog.show();
        }
    });

It is giving error on below line 它在下面的行给出错误

String[] delete = names2.get(i);
java.lang.IndexOutOfBoundsException:     Invalid index 1, size is 1

It is giving error on below line 它在下面的行给出错误

 String[] delete = names2.get(i); 

The names2 arraylist has just one element (size 1) but you're trying to get() the second one (index 1). names2只有一个元素(大小为1),但是您试图get()第二个元素(索引为1)进行get() )。

The code doesn't show how you set up your names2 list but anyway the len constraint for i index looping comes from mListView.getCount() which possibly isn't correct. 该代码未显示如何设置names2列表,但无论如何, i索引循环的len约束来自mListView.getCount() ,这可能是不正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM