Android 在listview中使用多选模式时从数据库中删除了错误的行
我试图修改连接到数据库的listview示例。它被设置为只允许选择listview中的一个项目,然后让用户单击delete person按钮。我做了一些更改,我认为这些更改将允许用户选择多个人,然后单击“删除”,并使其选中“删除列表中的项目”,然后当用户单击“删除”按钮时,通过“for”循环和“for”检查项目,以获取其id并调用数据库并从数据库中删除该行或项目桌子代码运行并删除项目,但删除了错误的项目,并且仅删除了部分选定项目。我知道这与我检查列表中选中项目的id的方式有关,但我不知道如何修复它。请参阅onDeleteClick方法中的代码 主要活动的源代码:Android 在listview中使用多选模式时从数据库中删除了错误的行,android,database,listview,delete-row,multiple-select,Android,Database,Listview,Delete Row,Multiple Select,我试图修改连接到数据库的listview示例。它被设置为只允许选择listview中的一个项目,然后让用户单击delete person按钮。我做了一些更改,我认为这些更改将允许用户选择多个人,然后单击“删除”,并使其选中“删除列表中的项目”,然后当用户单击“删除”按钮时,通过“for”循环和“for”检查项目,以获取其id并调用数据库并从数据库中删除该行或项目桌子代码运行并删除项目,但删除了错误的项目,并且仅删除了部分选定项目。我知道这与我检查列表中选中项目的id的方式有关,但我不知道如何修复
public class MainActivity extends ListActivity {
private static final int ADD_DIALOG = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView list = getListView();
list.setItemsCanFocus(false);
//list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
// initialize the adapter
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
//android.R.layout.simple_list_item_single_choice,
android.R.layout.simple_list_item_multiple_choice,
null,
new String[]{DBHelper.C_NAME},
new int[]{android.R.id.text1});
setListAdapter(adapter);
updateAdapterData();
}
public void updateAdapterData(){
// re-query the data
SQLiteDatabase db = new DBHelper(this).getReadableDatabase();
//SQLiteDatabase db = new DBHelper(this).getWritableDatabase();
Cursor c = db.query(DBHelper.TABLE_PEOPLE, null, null, null, null,
null, null);
((SimpleCursorAdapter)getListAdapter()).changeCursor(c);
db.close();
}
public void onAddClicked(View view){
showDialog(ADD_DIALOG);
}
public void onDeleteClicked(View view){
ListView lstView = getListView(); // added for hw
//int position = getListView().getCheckedItemPosition(); // removed for hw
SQLiteDatabase db = new DBHelper(this).getWritableDatabase();
for(int i = 0; i < lstView.getCount(); i++){
if(lstView.isItemChecked(i)){
long itemId = lstView.getItemIdAtPosition(i);
int rowsAffected = db.delete(DBHelper.TABLE_PEOPLE,
DBHelper.C_ID + " = " + itemId, null);
}
}
db.close();
//if(rowsAffected > 0)
updateAdapterData();
//if(position >= 0){
//long itemId = getListAdapter().getItemId(position);
//SQLiteDatabase db = new DBHelper(this).getWritableDatabase();
//int rowsAffected = db.delete(DBHelper.TABLE_PEOPLE, DBHelper.C_ID
+ " = " + itemId, null);
//db.close();
//if(rowsAffected > 0)
// updateAdapterData();
//}
}
protected Dialog onCreateDialog(int id){
Dialog d;
switch(id){
case ADD_DIALOG:
d = new Dialog(this);
d.setContentView(R.layout.add_person_dialog);
d.setTitle("Add a Person");
final TextView nameText = (TextView)d.findViewById(R.id.name);
d.findViewById(R.id.okay_btn).setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
addPerson(nameText.getText().toString());
dismissDialog(MainActivity.ADD_DIALOG);
}
});
d.findViewById(R.id.cancel_btn).setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
dismissDialog(MainActivity.ADD_DIALOG);
}
});
break;
default:
d = super.onCreateDialog(id);
break;
}
return d;
}
public void addPerson(String name){
// add the new data to the db
DBHelper helper = new DBHelper(this);
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(DBHelper.C_NAME, name);
db.insert(DBHelper.TABLE_PEOPLE, null, cv);
db.close();
// update the view
updateAdapterData();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Person"
android:onClick="onAddClicked"
/>
<Button
android:id="@+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Person"
android:onClick="onDeleteClicked"
/>
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>
主要活动的布局:
public class MainActivity extends ListActivity {
private static final int ADD_DIALOG = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView list = getListView();
list.setItemsCanFocus(false);
//list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
// initialize the adapter
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
//android.R.layout.simple_list_item_single_choice,
android.R.layout.simple_list_item_multiple_choice,
null,
new String[]{DBHelper.C_NAME},
new int[]{android.R.id.text1});
setListAdapter(adapter);
updateAdapterData();
}
public void updateAdapterData(){
// re-query the data
SQLiteDatabase db = new DBHelper(this).getReadableDatabase();
//SQLiteDatabase db = new DBHelper(this).getWritableDatabase();
Cursor c = db.query(DBHelper.TABLE_PEOPLE, null, null, null, null,
null, null);
((SimpleCursorAdapter)getListAdapter()).changeCursor(c);
db.close();
}
public void onAddClicked(View view){
showDialog(ADD_DIALOG);
}
public void onDeleteClicked(View view){
ListView lstView = getListView(); // added for hw
//int position = getListView().getCheckedItemPosition(); // removed for hw
SQLiteDatabase db = new DBHelper(this).getWritableDatabase();
for(int i = 0; i < lstView.getCount(); i++){
if(lstView.isItemChecked(i)){
long itemId = lstView.getItemIdAtPosition(i);
int rowsAffected = db.delete(DBHelper.TABLE_PEOPLE,
DBHelper.C_ID + " = " + itemId, null);
}
}
db.close();
//if(rowsAffected > 0)
updateAdapterData();
//if(position >= 0){
//long itemId = getListAdapter().getItemId(position);
//SQLiteDatabase db = new DBHelper(this).getWritableDatabase();
//int rowsAffected = db.delete(DBHelper.TABLE_PEOPLE, DBHelper.C_ID
+ " = " + itemId, null);
//db.close();
//if(rowsAffected > 0)
// updateAdapterData();
//}
}
protected Dialog onCreateDialog(int id){
Dialog d;
switch(id){
case ADD_DIALOG:
d = new Dialog(this);
d.setContentView(R.layout.add_person_dialog);
d.setTitle("Add a Person");
final TextView nameText = (TextView)d.findViewById(R.id.name);
d.findViewById(R.id.okay_btn).setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
addPerson(nameText.getText().toString());
dismissDialog(MainActivity.ADD_DIALOG);
}
});
d.findViewById(R.id.cancel_btn).setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
dismissDialog(MainActivity.ADD_DIALOG);
}
});
break;
default:
d = super.onCreateDialog(id);
break;
}
return d;
}
public void addPerson(String name){
// add the new data to the db
DBHelper helper = new DBHelper(this);
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(DBHelper.C_NAME, name);
db.insert(DBHelper.TABLE_PEOPLE, null, cv);
db.close();
// update the view
updateAdapterData();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Person"
android:onClick="onAddClicked"
/>
<Button
android:id="@+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Person"
android:onClick="onDeleteClicked"
/>
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>
“添加人员”对话框的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/okay_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Okay"
/>
<Button
android:id="@+id/cancel_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel"
/>
</LinearLayout>
</LinearLayout>
尝试使用getCheckedItemIds