Java 如何从数据库中删除项目
我正在制作一个应用程序,在数据库中存储名称和标记,并在列表视图中显示。如何在长按listview中的项目时删除listview中的项目以及数据库中的项目 以下是数据库帮助器的代码:Java 如何从数据库中删除项目,java,android,database,listview,Java,Android,Database,Listview,我正在制作一个应用程序,在数据库中存储名称和标记,并在列表视图中显示。如何在长按listview中的项目时删除listview中的项目以及数据库中的项目 以下是数据库帮助器的代码: public class PersonDatabaseHelper { private static final String TAG = PersonDatabaseHelper.class.getSimpleName(); // database configuration // if
public class PersonDatabaseHelper {
private static final String TAG = PersonDatabaseHelper.class.getSimpleName();
// database configuration
// if you want the onUpgrade to run then change the database_version
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "mydatabase.db";
public static String query;
// table configuration
private static final String TABLE_NAME = "person_table"; // Table name
private static final String PERSON_TABLE_COLUMN_ID = "_id"; // a column named "_id" is required for cursor
private static final String PERSON_TABLE_COLUMN_NAME = "person_name";
private static final String PERSON_TABLE_COLUMN_PIN = "person_pin";
private DatabaseOpenHelper openHelper;
private SQLiteDatabase database;
// this is a wrapper class. that means, from outside world, anyone will communicate with PersonDatabaseHelper,
// but under the hood actually DatabaseOpenHelper class will perform database CRUD operations
public PersonDatabaseHelper(Context aContext) {
openHelper = new DatabaseOpenHelper(aContext);
database = openHelper.getWritableDatabase();
}
public void insertData (String aPersonName, String aPersonPin) {
// we are using ContentValues to avoid sql format errors
ContentValues contentValues = new ContentValues();
contentValues.put(PERSON_TABLE_COLUMN_NAME, aPersonName);
contentValues.put(PERSON_TABLE_COLUMN_PIN, aPersonPin);
database.insert(TABLE_NAME, null, contentValues);
}
public Cursor getAllData () {
String buildSQL = "SELECT * FROM " + TABLE_NAME;
Log.d(TAG, "getAllData SQL: " + buildSQL);
return database.rawQuery(buildSQL, null);
}
// this DatabaseOpenHelper class will actually be used to perform database related operation
private class DatabaseOpenHelper extends SQLiteOpenHelper {
public DatabaseOpenHelper(Context aContext) {
super(aContext, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
// Create your tables here
String buildSQL = "CREATE TABLE " + TABLE_NAME + "( " + PERSON_TABLE_COLUMN_ID + " INTEGER PRIMARY KEY, " +
PERSON_TABLE_COLUMN_NAME + " TEXT, " + PERSON_TABLE_COLUMN_PIN + " TEXT )";
Log.d(TAG, "onCreate SQL: " + buildSQL);
sqLiteDatabase.execSQL(buildSQL);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
// Database schema upgrade code goes here
String buildSQL = "DROP TABLE IF EXISTS " + TABLE_NAME;
Log.d(TAG, "onUpgrade SQL: " + buildSQL);
sqLiteDatabase.execSQL(buildSQL); // drop previous table
onCreate(sqLiteDatabase); // create the table from the beginning
}
}
public void average() {
String query = "SELECT AVG("+PERSON_TABLE_COLUMN_PIN +") FROM "+TABLE_NAME;
database.rawQuery(query, null);
}
}
好的,因为您的数据库中的每个条目都有一个唯一的ID,所以您可以基于此进行删除,如下所示:
public void deleteItem(int itemId){
this.getWritableDatabase().delete(TABLE_NAME, PERSON_TABLE_COLUMN_ID + "=" + itemId, null);
}
然后,当长按列表视图中的某项时,可以调用该方法将其从数据库中删除,然后在适配器上调用删除(对象)
(或列表上的删除(int)
您的适配器使用的方法,然后调用notifyDataSetChanged()
以确保正确更新显示屏
编辑:要回答您关于如何将长时间单击侦听器应用于列表视图中的项目的问题,这取决于您是如何实现适配器的。如果是自定义适配器,只需将onLongClickListener()
设置为根视图(例如convertView)。如果您没有使用它,可以尝试将AdapterView.OnItemLongClickListener这样的附件连接到您的列表视图
:
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long arg3) {
// get the data from your list at position, i.e. data.get(position)
// then pass that object / id to your database helper, e.g.
Person p = data.get(position);
personDatabaseHelper.deleteItem(p.getId());
data.remove(position); // or adapter.remove(p), depends on your implementation
adapter.notifyDataSetChanged(); // remove the object from your Adapter and notify it of the change
return true;
}
});
你尝试了什么但没有成功?到底是什么问题?检测长新闻?从数据库中删除条目?还有什么?要从数据库中删除条目,请参阅以反映对listview的更改不要忘记调用listview.notifyDataSetChanged()
在StackOverflow中,通常最好一次只问一个问题。所以对于你的问题,我会把它分成两个问题:一个是db,一个是长新闻。对于SQLite db部分,我建议使用这样的类。它是一个SQLiteManager
类,它围绕着一个内部类SQLiteHelper
:我已经尝试过了,问题是我不能使用getWritableDatabase(),因为它说:“无法解析方法getWritableDatabase”,请尝试将“this.getWritableDatabase())更改为“database”“由于PersonDatabaseHelper类在实例化时已经定义了一个数据库实例。谢谢,它已经起作用了。”。长按listview中的一项,如何从MainActivity调用公共void deleteItem?你能给我看一下代码吗?我试着在主要帖子中回答这个问题,希望这能有所帮助!谢谢,但是如果我有一个定制的适配器,怎么做呢?
public View getView(final int position, View convertView, ViewGroup parent){
// your other stuff here
convertView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Person p = getItem(position);
personDatabaseHelper.deleteItem(p.getId());
remove(p);
notifyDataSetChanged();
return true;
}
});
return convertView;
}