如何从android中的表中删除一行?

时间:2013-03-01 13:23:24

标签: android database

这是在Handler类中使用的删除方法:

    public void deleteRow(long rowId){
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_CONTACTS, KEY_ID + "=" + rowId, null);
  }
  1. 我想从列表视图中删除所选行。这是我的ListView代码,它没有在logcat中显示任何错误,但没有删除该行。可能是什么问题?:

    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                final int position, long id) {
            TextView textView = (TextView) view
                    .findViewById(android.R.id.text1);
            String s = textView.getText().toString();
    
    
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);
            alertDialogBuilder.setTitle("Delete?");
            alertDialogBuilder
                    .setMessage("Click yes to delete")
                    .setCancelable(false)
    
                    .setNegativeButton("No",
                            new DialogInterface.OnClickListener() {
    
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method stub
                                    dialog.cancel();// close the dialog and
                                                    // do nothing..
                                }
                            })
                    .setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
    
                                public void onClick(DialogInterface dialog,
                                        int which) {
    
    
                                    // TODO Auto-generated method stub
    
                                    db = new SQLiteHandler(context);
                                    db.open();
                                    db.deleteRow(position);
                                    db.close();
    
                                }
                            });
    
            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();
    
            // show it
            alertDialog.show();
        }
    });
    

2 个答案:

答案 0 :(得分:0)

    Try this:

    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_CONTACTS, KEY_ID + "= ?"  , new String[] { ""+rowId });

答案 1 :(得分:0)

db.delete(TABLE_CONTACTS, KEY_ID + "=?", new String[] {String.valueOf(rowId)});