Android 删除行后SQLite数据库未正确更新

Android 删除行后SQLite数据库未正确更新,android,sqlite,android-listview,Android,Sqlite,Android Listview,当我删除任何数据时,listitem click在打开listitem时显示错误,并且自定义listview上的数据也不正确。删除第0行后,数据未正确更新。请帮忙 mydb = new DBHelper(this); Bundle extras = getIntent().getExtras(); if (extras != null) { int Value = extras.getInt("id"); if (Value > 0

当我删除任何数据时,listitem click在打开listitem时显示错误,并且自定义listview上的数据也不正确。删除第0行后,数据未正确更新。请帮忙

    mydb = new DBHelper(this);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        int Value = extras.getInt("id");
        if (Value > 0) {
            // means this is the view part not the add contact part.
            Cursor crs = mydb.getData(Value);
            id_To_Update = Value;
            crs.moveToFirst();
            String nam = crs.getString(crs.getColumnIndex(DBHelper.C_NAME));
            String phon = crs.getString(crs
                    .getColumnIndex(DBHelper.C_PHONE));
            String addr = crs.getString(crs
                    .getColumnIndex(DBHelper.C_ADDRESS));
            String dat = crs.getString(crs.getColumnIndex(DBHelper.C_DATE));
            String typ = crs.getString(crs.getColumnIndex(DBHelper.C_TYPE));
            if (!crs.isClosed()) {
                crs.close();
            }
            Button b = (Button) findViewById(R.id.button1);
            b.setVisibility(View.INVISIBLE);

            name.setText((CharSequence) nam);
            name.setFocusable(false);
            name.setClickable(false);

            phone.setText((CharSequence) phon);
            phone.setFocusable(false);
            phone.setClickable(false);

            type.setText((CharSequence) typ);
            type.setFocusable(false);
            type.setClickable(false);

            address.setText((CharSequence) addr);
            address.setFocusable(false);
            address.setClickable(false);

            date.setText((CharSequence) dat);
            date.setFocusable(false);
            date.setClickable(false);
        }
    }
}



    case R.id.Delete_Contact:

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.deleteContact)
                .setPositiveButton(R.string.yes,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                mydb.deleteContact(id_To_Update);
                                Toast.makeText(getApplicationContext(),
                                        "Deleted Successfully",
                                        Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(
                                        getApplicationContext(),
                                        com.example.addressbook.MainActivity.class);
                                startActivity(intent);
                            }
                        })
                .setNegativeButton(R.string.no,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                // User cancelled the dialog
                            }
                        });
        AlertDialog d = builder.create();
        d.setTitle("Are you sure");
        d.show();

        return true;
    default:
        return super.onOptionsItemSelected(item);

    }
}

public void run(View view) {
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        int Value = extras.getInt("id");
        if (Value > 0) {
            if (mydb.updateContact(id_To_Update, name.getText().toString(),
                    phone.getText().toString(), type.getText().toString(),
                    address.getText().toString(), date.getText().toString())) {
                Toast.makeText(getApplicationContext(), "Updated",
                        Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getApplicationContext(),
                        com.example.addressbook.MainActivity.class);
                startActivity(intent);
            } else {
                Toast.makeText(getApplicationContext(), "not Updated",
                        Toast.LENGTH_SHORT).show();
            }
        } else {
            if (mydb.insertContact(name.getText().toString(), phone
                    .getText().toString(), type.getText().toString(),
                    address.getText().toString(), date.getText().toString())) {
                Toast.makeText(getApplicationContext(), "done",
                        Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "not done",
                        Toast.LENGTH_SHORT).show();
            }
            Intent intent = new Intent(getApplicationContext(),
                    com.example.addressbook.MainActivity.class);
            startActivity(intent);
        }
DBHelper.java

public Integer deleteContact(Integer id) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete("contacts", "id = ? ",
            new String[] { Integer.toString(id) });
}

}我终于找到了解决办法。我在自定义列表视图中有一个文本视图,它的表id对于每一行都是唯一的,删除该行后,它的值不会改变,因此我在不同的活动中打开该行的数据。

请告诉我们错误,并尝试查找生成它的代码…E/AndroidRuntime15486:原因:android.database.CursorIndexOutOfBoundsException:请求索引0,大小为0,删除行后显示错误。删除后,它实际上没有正确更新列表视图。请编辑您的帖子,只提供更新功能,好吗?并提供您正在使用的表的示例。您必须尝试删除某个表上的某条记录,该表本身为空,因此会收到消息。是否只删除第0行?如果是,您正在尝试删除id为0的条目,但这不一定是条目号0?