0

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

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

2 回答 2

0
    Try this:

    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_CONTACTS, KEY_ID + "= ?"  , new String[] { ""+rowId });
于 2013-03-01T13:29:08.333 回答
0
db.delete(TABLE_CONTACTS, KEY_ID + "=?", new String[] {String.valueOf(rowId)});
于 2013-03-01T13:30:16.483 回答