[英]Try to delete selected item from listview and DB but my code will delete the previous item rather than the selected item
更新的用於刷新列表視圖的代碼不起作用。
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
cd.open();
cd.deleteRow(id+1);
strings.remove(position);
lv_arr = (String[]) strings.toArray(new String[strings.size()]);
arrayAdapter.notifyDataSetChanged();
return false;
}
您正在從列表視圖傳遞所選項目的ID或位置,因此您應該知道列表視圖的項目是使用0位置初始化的,並且可能會存儲ID為1的項目,然后將其增加1。
因此,在這種情況下,可以通過將id增加1來刪除它,或者為選定的項目創建一個對象,然后將其傳遞來刪除。
對於第一種簡單情況,請查看以下答案。
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
Log.v("long clicked", "pos: " + position);
cd.open();
//long i=id;
// i--;
cd.deleteRow(id + 1);
strings.remove(position)
lv_arr = (String[]) strings.toArray(new String[strings.size()]);
arrayAdapter.notifyDataSetChanged();
// CView.setVisibility(ListView.VISIBLE);
// Intent intent=new Intent(SqlView.this,SqlView.class);
// startActivity(intent);
// ret();
Toast.makeText(getApplicationContext(),"You have deleted a workout!", Toast.LENGTH_LONG).show();
return false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.