android如何删除列表视图项按其在 sqlite 数据库中的 ID

标签: sqlite Java Android
发布时间: 2017/3/19 16:58:23
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我跟着教程在这里︰ 我和导师 todo 应用程序想要自定义的代码 nad build 训练应用程序。我的第一个变化是要删除其 ID 在数据库中 (在本例中正在被删除的名称) 的事情。这里是我当前的代码负责删除项目︰

 // FUNCTION for DELETING EXERCISE
 public void deleteExercise(View view) {
    final View parent = (View) view.getParent();
    AlertDialog dialog = new AlertDialog.Builder(this)
            .setMessage("Are you sure, you want to delete exercise?")
            .setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    TextView exerciseTextView = (TextView) parent.findViewById(R.id.exercise_name);
                    String exercise = String.valueOf(exerciseTextView.getText());
                    SQLiteDatabase db = mHelper.getWritableDatabase();
                    db.delete(ExerciseContract.ExerciseEntry.TABLE,
                            ExerciseContract.ExerciseEntry.COL_EXERCISE_NAME + " = ?",
                            new String[]{exercise});
                    db.close();
                    updateUI();
                }
            })
            .setNegativeButton("Cancel", null)
            .create();
    dialog.show();
}

// UPDATING USER INTERFACE AFTER CHANGES IN DB
private void updateUI() {
    ArrayList<String> exerciseList = new ArrayList<>();
    SQLiteDatabase db = mHelper.getReadableDatabase();
    String[] projection = {
            ExerciseContract.ExerciseEntry._ID,
            ExerciseContract.ExerciseEntry.COL_EXERCISE_NAME,
            //ExerciseContract.ExerciseEntry.COL_EXERCISE_DESCRIPTION
    };
    Cursor cursor = db.query(
            ExerciseContract.ExerciseEntry.TABLE, //tablica do zapytań
            projection, //zwracane kolumny
            null,  //columny dla WHERE
            null, //wartosci dla WHERE
            null,//nie grupuj wierszy
            null,//nie filtruj grup
            null); //porządek sortowania
    while (cursor.moveToNext()) {
        int idx = cursor.getColumnIndex(ExerciseContract.ExerciseEntry.COL_EXERCISE_NAME);
        exerciseList.add(cursor.getString(idx));
    }

    if (mAdapter == null) {
        mAdapter = new ArrayAdapter<>(this,
                R.layout.item_workout,
                R.id.exercise_name,
                exerciseList);
        mWorkoutListView.setAdapter(mAdapter);
    } else {
        mAdapter.clear();
        mAdapter.addAll(exerciseList);
        mAdapter.notifyDataSetChanged();
    }

    cursor.close();
    db.close();
}

最简单的方式做到这一点会是什么?

解决方法 1:

您不返回的 id 在任何地方,我会亲自推荐自定义 adapter 。但我认为最容易做的快速方法是删除它,当用户单击列表中的项时 view

设置列表视图 onItemClickListener。

        mTaskListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Tasks task =(Tasks) mTaskListView.getItemAtPosition(position);

            deleteTask(task.getId());
        }
    });

删除任务通过在单击项 _id。

    public void deleteTask(long id) {

    //TextView taskTextView = (TextView) parent.findViewById(R.id.task_title);
    //String task = String.valueOf(taskTextView.getText());
    SQLiteDatabase db = mHelper.getWritableDatabase();
    db.delete(TaskContract.TaskEntry.TABLE, TaskContract.TaskEntry._ID + " = ?", new String[]{String.valueOf(id)});
    db.close();
    updateUI();
}

在 UpdateUI 部分更改这 while 循环也检索 _id。

      while (cursor.moveToNext()) {
        int title = cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_TITLE);
        int _id = cursor.getColumnIndex(TaskContract.TaskEntry._ID);

        Tasks tasks = new Tasks();
        tasks.setId(cursor.getInt(_id));
        tasks.setTitle(cursor.getString(title));
        taskList.add(tasks);

    }

最后创建一个模型为您的任务。

package com.aziflaj.todolist.db;

public class Tasks {

    String title;
    int id;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString()
    {
        return getTitle();
    }
}

也别忘了删除 button 从 LayoutFile。您可以显示 dialog 之前删去,但这是一个快速的解决方案。理想的情况下建议创建自己的自定义 adapter 。你可能要这样做,如果您想要保留 button 中并将其删除这种方式。

自定义 Adapter 为列表View

赞助商