gpt4 book ai didi

android - 如何删除 ListView 和数据库中的项目 - Android Studio

转载 作者:搜寻专家 更新时间:2023-10-30 23:44:38 27 4
gpt4 key购买 nike

我的 Activity 代码

请帮助我如何在 Android Studio 中删除连接到数据库的 ListView 中的项目。这是我的代码:

mainListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {            public boolean onItemLongClick(AdapterView parent, View view, final int position, final long id) {                final AlertDialog.Builder b = new AlertDialog.Builder(UserList.this);                b.setIcon(android.R.drawable.ic_dialog_alert);                b.setMessage("Ingin menghapus data?");                b.setPositiveButton("Ya",                        new DialogInterface.OnClickListener() {                            public void onClick(DialogInterface dialog, int whichButton) {                                IDTable = IDList.get(position);                                userList.remove(position);                                UserList.this.listAdapter.notifyDataSetChanged();                                InfoPokok info = new InfoPokok();                                info.setId(IDTable);                                System.out.println("ID : " + info.getId());                                infoPokokDao.deleteInfoPokok(info);                            }                        });                b.setNegativeButton("Tidak",                        new DialogInterface.OnClickListener() {                            public void onClick(DialogInterface dialog, int whichButton) {                                dialog.cancel();                            }                        });                b.show();                return true;            }        });

道:

public void deleteInfoPokok(InfoPokok infoPokok) {String id = infoPokok.getId() + "";        long deleteId = database.delete(MySQLiteHelper.TABLE_INFO_POKOK, MySQLiteHelper.COLUMN_ID                + " =?", new String[]{id});        Cursor cursor = database.query(MySQLiteHelper.TABLE_INFO_POKOK,                allColumns, MySQLiteHelper.COLUMN_ID + " = " + deleteId, null,                null, null, null);        cursor.moveToFirst();        cursor.close();    }

最佳答案

在你的 DISPLAYACTIVITY.JAVA 中

 btnDelete.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

DataBaseHandler db = new DataBaseHandler(DisplayImageActivity.this);

Log.d("Delete Image: ", "Deleting.....");
db.deleteCloth(new Cloth(imageId));

Intent i = new Intent(DisplayImageActivity.this, MainActivity.class);
startActivity(i);
finish();
}
});

然后在 DATABASEHANDLER.JAVA 中

 public void deleteCloth(Cloth cloth) {

SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CLOTHES, KEY_ID + " = ?", new String[] { String.valueOf(cloth.getID()) });
db.close();
}

关于android - 如何删除 ListView 和数据库中的项目 - Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30480397/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com