我的dbhelper添加条目可以很好地工作,但是当我单击每个条目中的"删除"按钮时,我想删除条目。我将"删除" onClick
事件设置为按钮,但我不知道该条目的ID或其他信息将其从数据库中删除。有人可以帮我吗?这是Android Studio。该应用程序是关于添加带有标题和注释的费用条目。我正在尝试在每个条目以外实现删除功能按钮。
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);
}
}
int result = sqLiteDatabase.delete(DBSchema.DATABASE_TABLE_NAME, DBSchema.DATABASE_ROW_ID + "=?",
new String[]{String.valueOf(whichOne)});
在哪里,哪个是ID列(PK)