如何获取seletect行表单列表视图,然后从Sqlite数据库中删除

时间:2016-12-15 11:19:57

标签: java android sqlite listview android-studio

我是Android新手。我有一个问题,当我通过复选框选择多行时,它将删除最后一行而不是第一行,当我选择整个Listview时,它正确删除所有列表.... 这是我的主要活动代码

private void showDialog(final String warning, final String s) {
    AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
    adb.setTitle(warning);

    adb.setMessage(s);
    adb.setIcon(R.drawable.deleteicon);

    String btn = "Ok";
    if (warning.equals("Warning")) {
        btn = "Yes";

        adb.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

    }

    adb.setPositiveButton(btn, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (warning.equals("Warning")) {
                int size = image_adapter.checkStatesArray.length;
                for (int i = size - 1; i > 0; i--) {
                    if (image_adapter.checkStatesArray[i] == true) {
                        db.deleteImage(i);
                    }
                    image_adapter.notifyDataSetChanged();
                }

                // listView.clearChoices();
                UpdateList();

                /*    sparseBooleanArray=listView.getCheckedItemPositions();
                for (int i=0;i<sparseBooleanArray.size();i++){
                    if (sparseBooleanArray.valueAt(i)) {
                        int key = (int) image_adapter.getItemId(i);
                        db.deleteImage(key);
                        //db.deleteImage(key);
                }*/
            }
        }
    });
    adb.show();
}

这是Adapter类代码。我将复选框的状态分配给布尔数组

 viewholder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int id=(Integer)buttonView.getTag();
            Toast.makeText(context, "clickd position"+id, Toast.LENGTH_SHORT).show();
            images.get(id).setSelected(buttonView.isChecked());
            if (buttonView.isChecked()){
                checkStatesArray[id]=true;
            }
            else {
                checkStatesArray[id] = false;
            }
        }
    });

数据库删除功能。这里

public void deleteImage(int id){
    SQLiteDatabase db=this.getReadableDatabase();
    Cursor c=this.getAll();
    //database=this.getReadableDatabase();
    //database.delete(TABLE_NAME,"id="+id,null);

    c.moveToFirst();
    for (int i=0;i<c.getCount();i++){
        id=c.getInt(c.getColumnIndex("id"));
        c.moveToNext();
    }
    db.delete(TABLE_NAME,"id="+id,null);
}

1 个答案:

答案 0 :(得分:0)

在deleteImage方法中,你得到for循环中的所有id。你将读取所有元素,最后一个将是你要删除的id。 将代码更改为以下。

 public void deleteImage(int id){
    SQLiteDatabase db=this.getReadableDatabase();
    db.delete(TABLE_NAME,"id="+id,null);
}