使用DBHelper删除listview上的特定行

时间:2014-01-16 07:49:58

标签: android database listview row sql-delete

您好我已经完成了使用imagebutton显示自定义列表视图。我想使用该按钮删除选定的列表视图行。我尝试在我的dbhelper上使用void但是我遇到了错误,当我使用游标时它会崩溃。我不想使用持有人。这个助手有什么解决方案吗?非常感谢你提前

数据库助手类:

private DatabaseOpenHelper openHelper;
    private SQLiteDatabase database;


    public PersonDatabaseHelper(Context aContext) {

        openHelper = new DatabaseOpenHelper(aContext);
        database = openHelper.getWritableDatabase();
    }

    public void insertData (String bbi, String total, String liter, String asup) {

        // we are using ContentValues to avoid sql format errors

        ContentValues contentValues = new ContentValues();

        contentValues.put(HASIL_COLUMN_BBI, bbi);
        contentValues.put(HASIL_COLUMN_TOTAL, total);
        contentValues.put(HASIL_COLUMN_LITER, liter);
        contentValues.put(HASIL_COLUMN_ASUP, asup);

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

    public Cursor deleteRow(String id){
        String buildSQL = "DELETE * FROM TABLE_NAME WHERE HASIL_COLUMN_ID"+"= ?"+id;

        Log.d(TAG, "deleterRow SQL: " + buildSQL);

        return database.rawQuery(buildSQL, null);
    }

    // this DatabaseOpenHelper class will actually be used to perform database related operation 

    public 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 + "( " + HASIL_COLUMN_ID + " INTEGER PRIMARY KEY, " +
                    HASIL_COLUMN_BBI + " INTEGER, " + HASIL_COLUMN_TOTAL + " REAL, "+ HASIL_COLUMN_LITER+ " NUMERIC," + HASIL_COLUMN_ASUP+" INTEGER )";

            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 delete_kontak(SQLiteDatabase sqLiteDatabase){

        sqLiteDatabase.execSQL("DELETE * FROM TABLE_NAME WHERE HASIL_COLUMN_ID > -1");
    }
}

ListView活动:

databaseHelper = new PersonDatabaseHelper(this);

        delete = (ImageButton) findViewById (R.id.btn_hapus);

        listView = (ListView) findViewById(R.id.list_data);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d(TAG, "clicked on item: " + position);
            }

        });
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                customAdapter = new CustomCursorAdapter(MenuHistory.this, databaseHelper.getAllData());
                listView.setAdapter(customAdapter);
            }
        });
        // this is the imagebutton "delete"
        delete.setOnClickListener(new OnClickListener() {

            public void onClick() {
                customAdapter = new CustomCursorAdapter(MenuHistory.this, databaseHelper.deleteRow(null));
                delete.setAdapter(customAdapter);
            }

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

            }
        });

        }

        public void onClickEnterData(View btnAdd) {

            startActivityForResult(new Intent(this, EnterDataActivity.class), ENTER_DATA_REQUEST_CODE);

        }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == ENTER_DATA_REQUEST_CODE && resultCode == RESULT_OK) {

databaseHelper.insertData(data.getExtras().getString("tag_Hasil_BBI"), data.getExtras().getString("tag_Hasil_Total")
                    , data.getExtras().getString("tag_Hasil_Liter"), data.getExtras().getString("tag_Jumlah_Asupan"));

            customAdapter.changeCursor(databaseHelper.getAllData());
        }
    }
}

0 个答案:

没有答案