【问题标题】:Delete rows specific row from sqlite从 sqlite 中删除行特定的行
【发布时间】:2014-08-05 13:18:53
【问题描述】:

我不确定我是否了解如何使用 listview.setOnLongClick 从数据库中删除特定行...

我想使用从 onLongClick“位置”参数获得的位置删除 db 中的行,但它们彼此不对应:

列表中的位置 = 1 ----> db 中的位置(_id 自动递增)可能是 4 或 6,或者谁知道。

那么我如何确保我在列表视图和数据库本身中都有相应的位置?

这是我的代码:

数据库适配器

public class MySQLiteHelper extends SQLiteOpenHelper {

    public static final String TABLE_NAME = "notes";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_TEXT = "text";

    public static final String DATABASE_NAME = "santa.db";
    private static final int DATABASE_VERSION = 1;

    public int InsertNote(String note) {

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(COLUMN_TEXT, note);
        long success = db.insert(TABLE_NAME, null, values);

        Log.d("DB", "INSERTED INTO POSITION " + success);
        db.close();

        return (int) success;
    }

    public String GetNote(int position) {

        SQLiteDatabase db = this.getWritableDatabase();

        String getQuery = "SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_ID
                + "=" + String.valueOf(position);

        // Log.d("DB", "QUERY = " + getQuery);

        Cursor cursor = db.rawQuery(getQuery, null);

        if (cursor != null && cursor.moveToFirst()) {
            // Log.d("DB", "FIRST AND NOT NULL");
        }

        // Log.d("DB", "" + cursor.getCount());
        String get = cursor.getString(cursor.getColumnIndex(COLUMN_TEXT));
        // Log.d("GOT NOTE", "Got Note - " + get);

        cursor.close();

        return get;

    }

    // Returns amount of rows
    public int GetSize() {
        SQLiteDatabase db = this.getWritableDatabase();
        String queryRows = "SELECT * FROM " + TABLE_NAME;
        Cursor c = db.rawQuery(queryRows, null);
        int size = c.getCount();
        return size;
    }

    public Cursor GetAllNotes() {

        SQLiteDatabase db = this.getWritableDatabase();

        String getQuery = "SELECT * FROM " + TABLE_NAME;

        Cursor cursor = db.rawQuery(getQuery, null);

        return cursor;

    }

    public Boolean DeleteNote(int position) {

        position=+1;

        Boolean success = false;
        SQLiteDatabase db = this.getWritableDatabase();

        String deleteQuery = "DELETE FROM " + TABLE_NAME + " WHERE "
                + COLUMN_ID + "=" + position;

        Log.d("DELETE", deleteQuery);

        Cursor c = db.rawQuery(deleteQuery, null);
        if (c.getCount() > 0) {
            success = true;
        }

        return success;
    }

    // Database creation sql statement
    private static final String DATABASE_CREATE = "create table " + TABLE_NAME
            + "(" + COLUMN_ID + " integer primary key autoincrement, "
            + COLUMN_TEXT + " text not null);";

    public MySQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(MySQLiteHelper.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + COLUMN_TEXT);
        onCreate(db);
    }

}

我从中获取项目在列表中的位置并删除一行的“setOnItemClickListener”:

list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, View view,
                int position, long arg) {


            Boolean success = db.DeleteNote(position);
            if (success) {
                Toast.makeText(getActivity(),
                        getString(R.string.deleteToast), Toast.LENGTH_SHORT)
                        .show();
            }

            else {
                Toast.makeText(getActivity(),
                        getString(R.string.deleteToastFail),
                        Toast.LENGTH_SHORT).show();
            }
            mAdapter.notifyDataSetChanged();

        }
    });

【问题讨论】:

  • 制作一个自定义列表视图并添加两个文本视图,其中一个应为 1 sp(表示不可见),其他您喜欢.. 在表中添加一个 id 字段并在列表视图中加载 id 和值分配id 到 1 sp textview 和其他文本到普通 textview .. 这样你可以将 listview 中的 id 对应到 table 中的 .. 你必须为 id 和其他数据字段创建一个 getter 和 setter 类。

标签: android sqlite android-listview


【解决方案1】:

当您从数据库中获取行时,请使用您的 id 列。将其存储在 listview 项目的标签中,并在触发点击时从标签中读取 id。

为了做到这一点,您需要将注释作为对象而不是字符串。

class Note {
    private long id;
    private String note;

    // getters and setters
}

【讨论】:

  • 你说得对,我需要使用监听器提供的“id”而不是“位置”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多