【问题标题】:SQLite Database not updating properly after deleting row删除行后 SQLite 数据库未正确更新
【发布时间】:2014-07-06 22:40:15
【问题描述】:

当我删除任何数据时,listitem 点击在打开 listitem 时显示错误,并且自定义列表视图上的数据也不正确。删除第 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) });
}

}

【问题讨论】:

  • 请给我们错误,并尝试找到产生它的代码...
  • E/AndroidRuntime(15486): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 删除行后显示错误。删除后实际上并没有正确更新列表视图。
  • 能否请您编辑您的帖子并只给我们更新的功能?并提供您正在使用的表格样本
  • 您必须尝试删除自己为空的表上的某些记录,因此您收到的消息。
  • 您是否可能只想删除第 0 行?如果是,您正在尝试删除 id=0 的条目,但这不一定是条目号 0?

标签: android sqlite android-listview


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 2014-07-12
    • 2021-06-22
    • 1970-01-01
    相关资源
    最近更新 更多