Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中从表中删除行?_Android_Database - Fatal编程技术网

如何在android中从表中删除行?

如何在android中从表中删除行?,android,database,Android,Database,这是m在处理程序类中使用的删除方法: public void deleteRow(long rowId){ SQLiteDatabase db = this.getWritableDatabase(); db.delete(TABLE_CONTACTS, KEY_ID + "=" + rowId, null); } 我想从列表视图中删除所选行。这是我的ListView代码,它不会在logcat中显示任何错误,但不会删除该行。!有什么问题吗 listView.setO

这是m在处理程序类中使用的删除方法:

    public void deleteRow(long rowId){
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_CONTACTS, KEY_ID + "=" + rowId, null);
  }
  • 我想从列表视图中删除所选行。这是我的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();
        }
    });
    
    listView.setOnItemClickListener(新的OnItemClickListener(){
    public void onItemClick(AdapterView父级、视图、,
    最终整数位置,长id){
    TextView TextView=(TextView)视图
    .findviewbyd(android.R.id.text1);
    字符串s=textView.getText().toString();
    AlertDialog.Builder alertDialogBuilder=新建AlertDialog.Builder(
    上下文);
    alertDialogBuilder.setTitle(“删除?”);
    alertDialogBuilder
    .setMessage(“单击是删除”)
    .setCancelable(错误)
    .setNegativeButton(“否”,
    新建DialogInterface.OnClickListener(){
    公共void onClick(对话框接口对话框,
    int(其中){
    //TODO自动生成的方法存根
    dialog.cancel();//关闭对话框并
    //什么也不做。。
    }
    })
    .setPositiveButton(“是”,
    新建DialogInterface.OnClickListener(){
    公共void onClick(对话框接口对话框,
    int(其中){
    //TODO自动生成的方法存根
    db=新的SQLiteHandler(上下文);
    db.open();
    db.deleteRow(位置);
    db.close();
    }
    });
    //创建警报对话框
    AlertDialog AlertDialog=alertDialogBuilder.create();
    //表现出来
    alertDialog.show();
    }
    });
    

  • 将密钥\u ID替换为“\u ID”n尝试..感谢您的回复..我得到了解决方案。。实际上,我需要传递“id”,但我在listView中传递了“position”.现在工作正常。。谢谢大家。。
        Try this:
    
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_CONTACTS, KEY_ID + "= ?"  , new String[] { ""+rowId });
    
    db.delete(TABLE_CONTACTS, KEY_ID + "=?", new String[] {String.valueOf(rowId)});