我有一个包含3个字段(id,name和type)的小表的数据库。 我尝试在列表视图的行中显示名称和类型,为此我使用了一个简单的适配器。主要问题是如何使用longclick contextmenu从listview(适配器和数据库)中删除项目。
usdbh = new bbdd(this);
db = usdbh.getWritableDatabase();
cur = db.rawQuery("SELECT id as _id, Tipo, Nombre FROM Equipos", null);
startManagingCursor(cur);
//String[] from=new String[] {"Tipo","Nombre"};
//int[] id_views=new int[]{R.id.Tipo,R.id.Nombre};
///
String[] columnas = new String[] {"Nombre", "Tipo"};
int[] id_views = new int[] {R.id.Nombre, R.id.Tipo};
adapter = new SimpleCursorAdapter(this, R.layout.row, cur, columnas, id_views);
String c = String.valueOf(cur.getCount());
//Toast.makeText(getApplicationContext(), c, Toast.LENGTH_LONG).show();
String count = String.valueOf(adapter.getCount());
Log.v("Count", count);
lv = (ListView) findViewById(R.id.list);
lv.setAdapter(adapter);
db.close();
//cur.close();
registerForContextMenu(lv);
这是使用longclick运行并选择删除
的功能int id = cur.getInt(0);
String sid = String.valueOf(id);
String equipo = cur.getString(2);
//Toast.makeText(getApplicationContext(), "Voy a borrar" + id + equipo , Toast.LENGTH_LONG).show();
String sql = "DELETE FROM Equipos WHERE id =" + id;
usdbh = new bbdd(this);
SQLiteDatabase db = usdbh.getWritableDatabase();
try {
db.execSQL(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
db.close();
//Delete from listview
lv.removeViewAt(id);
Thankz