如何使用数据库游标从list_view中删除条目

时间:2017-11-21 23:07:42

标签: android android-studio

我的DBHelper在添加条目时工作正常,但我想在每个条目中单击“删除”按钮时删除条目。我将“删除”onClick事件设置为按钮,但我不知道该条目的id或其他信息要从数据库中删除它。有人可以帮我吗? 这是android工作室。 这个应用程序是关于添加带有标题和注释的费用条目。我正在尝试实现每个条目旁边的删除功能按钮。

expense_entry.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">



    <TextView
        android:id="@+id/id_entry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:text="id"
        android:textColor="@android:color/holo_green_light"
        android:textSize="18sp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/title_entry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title_lable"
        android:textColor="@android:color/holo_green_light"
        android:textSize="18sp"
        android:layout_below="@+id/id_entry"
        android:layout_alignLeft="@+id/id_entry"
        android:layout_alignStart="@+id/id_entry" />


    <TextView
        android:id="@+id/note_entry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/note_lable"
        android:textColor="@android:color/holo_orange_light"
        android:textSize="18sp"
        android:layout_below="@+id/title_entry"
        android:layout_alignLeft="@+id/title_entry"
        android:layout_alignStart="@+id/title_entry" />

    <Button
        android:id="@+id/delete_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:onClick="delete"
        android:text="Delete" />



</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    Button button_add;
    ListView list_view;
    Context context = MainActivity.this;
    SimpleCursorAdapter adapter;
    DBHelper db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.d("insertinginging", "in main");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    db = new DBHelper(this);
    button_add = (Button) findViewById(R.id.add_button);
    list_view = (ListView) findViewById(R.id.list_layout);
    populateList();
    displayLogList();
}



private void displayLogList() {
    Log.d("insertinginging", "in display");
    try
    {
        Cursor cursor = db.getAllLogs();
        if (cursor == null)
        {
            return;
        }
        if (cursor.getCount() == 0)
        {
            return;
        }
        Log.d("insertinginging", cursor.getColumnName(0));
        Log.d("insertinginging", cursor.getColumnName(1));
        String[] columns = new String[] {
                db.KEY_ID,
                db.KEY_TITLE,
                db.KEY_NOTE
        };
        int[] boundTo = new int[] {
                R.id.id_entry,
                R.id.title_entry,
                R.id.note_entry

        };

        adapter = new SimpleCursorAdapter(this,
                R.layout.expense_entry,
                cursor,
                columns,
                boundTo,
                0);
        list_view.setAdapter(adapter);
    }
    catch (Exception ex)
    {
        Log.d("insertinginging", "catch?!");
    }

}

private void populateList() {
    db.addLog(new ExpenseLogEntryData(1,"lipstick","pat macgrath flesh3"));
}

public void delete(View v) {

    //what should I do to get this entry's id or position??
}

public void add(View v) {
    Intent addIntent = new Intent(this, expense_add.class);
    startActivityForResult(addIntent,1);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){
            db.addLog((ExpenseLogEntryData) data.getSerializableExtra("result"));
            Log.d("insertinginging", "added");
            displayLogList();
        }
    }
}

public void clear(View v) {
    db.deleteAllLogs();
    list_view.setAdapter(null);
}

  }

1 个答案:

答案 0 :(得分:0)

 int result = sqLiteDatabase.delete(DBSchema.DATABASE_TABLE_NAME, DBSchema.DATABASE_ROW_ID + "=?",
                new String[]{String.valueOf(whichOne)});

其中,whichOne是id列(pk)