3 つのフィールド (id、name、type) を持つ小さなテーブルを持つデータベースがあります。リストビューの行に名前とタイプを表示しようとしています。この目的のために、simplecursor アダプターを使用しています。主な問題は、ロングクリックのコンテキストメニューを使用してリストビュー(アダプターとデータベース)からアイテムを削除する方法です。
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);
これは、ロングクリックで実行され、削除を選択する機能です
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);
ありがとう