删除行后,SQLite数据库无法正确更新

j

当我删除任何数据时,当打开listitem时,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) });
}

}

j

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法从sqlite数据库中删除选定的行

来自分类Dev

无法删除SQLite数据库中的行

来自分类Dev

无法删除sqlite数据库

来自分类Dev

SQLIte数据库无法删除条目

来自分类Dev

从android SQLite数据库中删除行会在删除行后破坏条目

来自分类Dev

更新apk版本后删除了Android Sqlite数据库记录

来自分类Dev

如何从选定行datagridview中删除记录并在删除后更新数据库?

来自分类Dev

无法从数据库删除行

来自分类Dev

无法从数据库中删除行

来自分类Dev

更新后,Spring MVC无法显示正确的数据库值

来自分类Dev

无法更新数据库中的行

来自分类Dev

如何正确设置sqlite数据库中的删除方法?

来自分类Dev

我无法删除Sqlite数据库中的数据

来自分类Dev

在数据库中删除行后更新DbSet.Local

来自分类Dev

尽管数据库已成功更新,但删除行后表不会刷新

来自分类Dev

Android - 我无法从 SQLite 数据库更新数据

来自分类Dev

无法更新后数据库更新当前时间戳

来自分类Dev

SQLite 数据库在更新后被 C# 锁定?

来自分类Dev

无法从android中的数据库中删除行(where子句后sql查询为空)

来自分类Dev

在Android中删除或更新SQLite数据库中的条目

来自分类Dev

安装后无法访问SQLite数据库

来自分类Dev

安装后无法访问SQLite数据库

来自分类Dev

更改模型类后删除和更新数据库?

来自分类Dev

本周门户更新后如何删除DocumentDB数据库?

来自分类Dev

SQLite无法从数据库中删除值

来自分类Dev

SQLite无法从数据库中删除值

来自分类Dev

无法在iOS的sqlite数据库中删除表

来自分类Dev

PHP表单无法正确更新SQL数据库

来自分类Dev

更新SQLite数据库

Related 相关文章

热门标签

归档