从数据库和arraylist中删除行

时间:2014-01-30 08:40:28

标签: java android sqlite android-alertdialog

这是我迄今为止所做的事情;

活动类;

traineesListView
            .setOnItemLongClickListener(new OnItemLongClickListener() {

                @Override
                public boolean onItemLongClick(AdapterView<?> arg0,
                        View arg1, int arg2, final long person_id) {
                    currentTrainee = (Trainee) arg0.getAdapter().getItem(
                            arg2);
                    SharedMemory.getInstance().setCurrentTrainee(
                            currentTrainee);

                    String items[] = { "Edit your information",
                            "Unregister from training" };
                    Builder alert = new AlertDialog.Builder(
                            TraineeActivity.this);
                    alert.setTitle("Dear!" + " "
                            + currentTrainee.getFormatedName() + "," + " "
                            + "Choose your action!!");
                    alert.setItems(items, new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface arg0,
                                int selectedItem) {

                            if (selectedItem == 0) {

                                SharedMemory.getInstance()
                                        .setCurrentAction(
                                                SharedMemory.ACTION_EDIT);
                                SharedMemory.getInstance()
                                        .setCurrentTrainee(currentTrainee);

                                Intent intent = new Intent(
                                        TraineeActivity.this,
                                        FormActivity.class);
                                TraineeActivity.this.startActivity(intent);

                            } else if (selectedItem == 1) {
                                // delete trainee information
                                Builder alert = new AlertDialog.Builder(
                                        TraineeActivity.this);
                                alert.setTitle("Dear!" + " "
                                        + currentTrainee.getFormatedName());
                                alert.setMessage("Are you sure want to unregister from training?");
                                alert.setPositiveButton(
                                        "YES",
                                        new DialogInterface.OnClickListener() {

                                            @Override
                                            public void onClick(
                                                    DialogInterface arg0,
                                                    int arg1) {
                                                int training_id = SharedMemory
                                                        .getInstance()
                                                        .getCurrentTraining()
                                                        .getId();
                                                // Unregister Trainee
                                                currentTrainee
                                                        .unRegisterFromTraining(training_id);
                                                Log.i( "Training ID:"+Integer.toString(training_id), "Person ID:"+Integer.toString(currentTrainee.getId()));
                                                // Remove trainee from
                                                // ArrayList
                                            }
                                        });

                                alert.setNegativeButton(
                                        "NO",
                                        new DialogInterface.OnClickListener() {

                                            @Override
                                            public void onClick(
                                                    DialogInterface arg0,
                                                    int arg1) {
                                                arg0.cancel();

                                            }
                                        });
                                alert.show();

                            }

                        }

                    });
                    alert.show();
                    return true;
                }

            });

我的模特课:

public void unRegisterFromTraining(int training_id) {

    DatabaseHelper db = DatabaseHelper.getInstance();

    db.delete(DatabaseHelper.TABLE_ATTENDANCE, DatabaseHelper.TrainingId
            + "=" + training_id + "AND" + DatabaseHelper.PersonId+"= ?"
            ,new String[] {Integer.toString(this.getId())});

}

我的数据库类;

public void delete(String tableAttendance, String where,String[] whereArgs) {
    // TODO Auto-generated method stub

}

我无法从db表中删除该行,即使我看不到任何错误。我需要一些方法。对你来说可能很简单,但我无法弄清楚为什么?我们将非常感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

public void delete(String tableAttendance, String where,String[] whereArgs) {
    // TODO Auto-generated method stub

}

此方法实现为空,它什么都不做。您应该将其委托给SQLiteDatabase

public void delete(String tableAttendance, String where,String[] whereArgs) {
    db.delete(tableAttendance, where, whereArgs);
}

其中db是一个开放的SQLiteDatabase成员。

之后修复SQL语法:

db.delete(DatabaseHelper.TABLE_ATTENDANCE, DatabaseHelper.TrainingId
        + "=" + training_id + "AND" + DatabaseHelper.PersonId+"= ?"
        ,new String[] {Integer.toString(this.getId())});

您需要AND周围的空白区域。

答案 1 :(得分:1)

你可能在这里出错了。参数传递后,在AND之间提供空格并提供"=?"而不是单"="。更正如下

 db.delete(DatabaseHelper.TABLE_ATTENDANCE, DatabaseHelper.TrainingId
    + "= ?" + training_id + " AND " + DatabaseHelper.PersonId+"= ?"
    ,new String[] {Integer.toString(this.getId())});