如何从由SQLite支持的listView中删除项目并使用新内容更新listView?
我试图按照其他示例的解决方案,但似乎无法删除这些项目。
在listView中显示数据的类:
public class MyFeedsScreen extends ListActivity {
DatabaseHandler dbHandler;
SimpleCursorAdapter dataAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
//Set the layout of MyFeeds screen
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.my_feeds_screen);
dbHandler = new DatabaseHandler(MyFeedsScreen.this);
displayList();
}
public void displayList() {
//Call method to open database
dbHandler.getWritableDatabase();
//Open database and get instance of database handler
dbHandler = new DatabaseHandler(this);
final Cursor cursor = dbHandler.getData();
//Takes string array for title, link and description and maps them different textView items
String from[] = new String[]{dbHandler.columnTitle, dbHandler.link, dbHandler.description};
int to[] = new int[]{R.id.textView1};
dataAdapter = new SimpleCursorAdapter(this, R.layout.row_item, cursor, from, to, 0);
final ListView lv = getListView();
lv.setAdapter(dataAdapter);
lv.setLongClickable(true);
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
try {
dbHandler.open();
} catch (SQLException e) {
e.printStackTrace();
}
final String itemId = cursor.getString(cursor.getColumnIndex("_id"));
dbHandler.deleteItemFromTable(itemId);
dbHandler.getData();
dataAdapter.notifyDataSetChanged();
displayList();
Toast.makeText(MyFeedsScreen.this, "Item has been deleted from MyFeeds!", Toast.LENGTH_SHORT).show();
return true;
}
});
}//displayList()}//class
DatabaseHandler.java - 填充listview和删除项目的方法
public class DatabaseHandler extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "news_db";
public static SQLiteDatabase db;
private static final int DATABASE_VERSION = 43;
protected static final String TABLE_NEWS = "news";
private static final String TAG = "TAG";
private static final String TAG1 = "TAG1";
static String columnTitle = "title";
protected static final String _id = "_id";
protected static final String link = "link";
protected static final String description = "description";
//Methods for tables
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE_NEWS = "CREATE TABLE " + TABLE_NEWS + "(_id VARCHAR, title TEXT, link TEXT, description TEXT)";
db.execSQL(CREATE_TABLE_NEWS);
Log.w(TAG, "Database has been created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NEWS);
onCreate(db);
}
public void open() throws SQLException {
db = this.getWritableDatabase();
}
//Method to insert new rssItem object to the database
public void insertRssItem(RssItem rssItem) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("_id", rssItem._id);
contentValues.put("title", rssItem.title);
contentValues.put("link", rssItem.link);
contentValues.put("description", rssItem.description);
db.insert(TABLE_NEWS, "title", contentValues);
Log.d(TAG1, "ITEM SAVED IN DATABASE!!!");
}//insert item method
//get data from database for listview
public Cursor getData() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT _id, title, link, description FROM " + TABLE_NEWS, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
cursor.getString(cursor.getColumnIndexOrThrow(cursor.getColumnName(1)));
cursor.moveToNext();
}
return cursor;
}
//Method to delete item from database, on long-pressing listView
public void deleteItemFromTable(String rowId) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NEWS, _id + " = " + rowId, null);
Log.w("Item has been deleted", "*****************");
db.close();
}}
当我选择在屏幕上显示吐司的项目时,logcat显示&#34;项目已被删除&#34;日志标记。