2

我有一个包含 3 个字段(id、名称和类型)的小表的数据库。我尝试在列表视图的一行中显示名称和类型,为此我使用了一个简单的光标适配器。主要问题是如何使用长按上下文菜单从列表视图(适配器和数据库)中删除项目。

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);

谢谢

4

0 回答 0