通过从填充的列表视图中获取 id 来从 SQLite 数据库中删除一行

发布于 2024-12-29 08:40:48 字数 2565 浏览 4 评论 0原文

数据库中的各个行填充在自定义列表视图中。该行的 id 也显示在列表视图中。我想要的只是通过单击相应的列表来删除特定行。

这是我用来显示数据库内容的代码:

class NoteAdapter extends CursorAdapter {

    public NoteAdapter(Cursor c) {
        super(Reccuring.this, c);

    }

    @Override
    public void bindView(View row, Context ctxt, Cursor c) {
        NoteHolder holder = (NoteHolder) row.getTag();
        holder.populateFrom(c, helper);

    }

    @Override
    public View newView(Context ctxt, Cursor c, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.row1, parent, false);
        NoteHolder holder = new NoteHolder(row);
        row.setTag(holder);
        return row;
    }
}

static class NoteHolder {
    private TextView name = null;
    private TextView number = null;
    private TextView idtext = null;

    NoteHolder(View row) {
        name = (TextView) row.findViewById(R.id.noteText);
        number= (TextView) row.findViewById(R.id.noteText1);
        idtext = (TextView) row.findViewById(R.id.noteText3);

    }

    void populateFrom(Cursor c, NoteHelper helper) {
        name.setText(helper.getNote(c));
        number.setText(helper.getNote1(c));
        idtext.setText(helper.getNote2(c));

    }
}

这是我的数据库:

public class NoteHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "Test.db";
    private static final int SCHEMA_VERSION = 1;

    public NoteHelper(Context context) {
        super(context, DATABASE_NAME, null, SCHEMA_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE Notes (_id INTEGER PRIMARY KEY AUTOINCREMENT, note TEXT, num TEXT, sDate TEXT);");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}

    public void insert(String note, String amt, String sDate) {
        ContentValues cv = new ContentValues();
        cv.put("note", note);
        cv.put("num", num);
        cv.put("sDate", sDate);
        getWritableDatabase().insert("Notes", "notes", cv);
    }

    public String getNote(Cursor c) {
        return (c.getString(1));

    }

    public String getNote1(Cursor c) {
        return (c.getString(2));

    }

    public String getNote2(Cursor c) {
        return (c.getString(0));

    }



    public Cursor getAll() {
        return (getReadableDatabase().rawQuery(
                "SELECT _id, note, num, sDate FROM Notes", null));

        }

    }
}

如何选择列表视图中的行并删除数据库中的相应行。

Individual rows in the database are populated in a custom listview. the id of the row is also displayed in the listview. All I want is to delete a particular row by clicking on the corresponding list.

Here is the code I used to display the contents of the database:

class NoteAdapter extends CursorAdapter {

    public NoteAdapter(Cursor c) {
        super(Reccuring.this, c);

    }

    @Override
    public void bindView(View row, Context ctxt, Cursor c) {
        NoteHolder holder = (NoteHolder) row.getTag();
        holder.populateFrom(c, helper);

    }

    @Override
    public View newView(Context ctxt, Cursor c, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.row1, parent, false);
        NoteHolder holder = new NoteHolder(row);
        row.setTag(holder);
        return row;
    }
}

static class NoteHolder {
    private TextView name = null;
    private TextView number = null;
    private TextView idtext = null;

    NoteHolder(View row) {
        name = (TextView) row.findViewById(R.id.noteText);
        number= (TextView) row.findViewById(R.id.noteText1);
        idtext = (TextView) row.findViewById(R.id.noteText3);

    }

    void populateFrom(Cursor c, NoteHelper helper) {
        name.setText(helper.getNote(c));
        number.setText(helper.getNote1(c));
        idtext.setText(helper.getNote2(c));

    }
}

And here's my database:

public class NoteHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "Test.db";
    private static final int SCHEMA_VERSION = 1;

    public NoteHelper(Context context) {
        super(context, DATABASE_NAME, null, SCHEMA_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE Notes (_id INTEGER PRIMARY KEY AUTOINCREMENT, note TEXT, num TEXT, sDate TEXT);");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}

    public void insert(String note, String amt, String sDate) {
        ContentValues cv = new ContentValues();
        cv.put("note", note);
        cv.put("num", num);
        cv.put("sDate", sDate);
        getWritableDatabase().insert("Notes", "notes", cv);
    }

    public String getNote(Cursor c) {
        return (c.getString(1));

    }

    public String getNote1(Cursor c) {
        return (c.getString(2));

    }

    public String getNote2(Cursor c) {
        return (c.getString(0));

    }



    public Cursor getAll() {
        return (getReadableDatabase().rawQuery(
                "SELECT _id, note, num, sDate FROM Notes", null));

        }

    }
}

How can I select the rows in the listview and delete the corresponding rows in the database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

行雁书 2025-01-05 08:40:48

首先获取列表视图的onItemClickListener()方法单击的位置的值:

@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
    LinearLayout linearLayout = (LinearLayout) view;// The main layout which contains your child views in which value is displayed

    TextView name = (TextView)linearLayout.getChildAt(0);// get child textview 1

    String nametext = name.getText().toString();// get String out of those textview

    deleteOldData(nametext)   
}

然后转到您的数据库类并编写以下代码:

public void deleteOldData(String nametext) {

            _sqliteDB.delete(tablename, " note = '" + nametext + "'", null);

    }

这样您就可以删除表的特定行。

如果您无法执行此操作,或者数据库中的注释列可能相同,请使用唯一的 Id 在列表视图的文本视图中设置,以这种形式提取它,然后将该值传递给数据库代码,然后删除它。

First get the value at position which is clicked by method onItemClickListener() of list view:

@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
    LinearLayout linearLayout = (LinearLayout) view;// The main layout which contains your child views in which value is displayed

    TextView name = (TextView)linearLayout.getChildAt(0);// get child textview 1

    String nametext = name.getText().toString();// get String out of those textview

    deleteOldData(nametext)   
}

Then go to your database class and write this code:

public void deleteOldData(String nametext) {

            _sqliteDB.delete(tablename, " note = '" + nametext + "'", null);

    }

This way you can delete your table particular row.

If you are not able to do with this or may be notes column in database are same so use unique Id to set in text view of listview, extract it in this form and then pass that value to the database code and then delete it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文