Cursor smCursor = getContentResolver().query(
Uri.parse("content://contact"),
new String[] { "_id", "address", "body" }, "address like?",
new String[] { searchContactPhone }, null);
CustomAdapter ada = new CustomAdapter(this,
R.layout.sm_layout, smCursor, new String[] {
"body" }, new int[] {
R.id.txt2 });
lvSm.setAdapter(ada);
btnDel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ArrayList<Boolean> ca = ada.getItemsThatAreChecked();
// this ArrayList will hold our Strings from the rows that were clicked.
ArrayList<String> results = new ArrayList<String>();
int s = ca.size();
for (int i = 0; i < s; i++) {
if (ca.get(i).booleanValue()) {
smCursor.moveToPosition(i);
Log.v("sss", smCursor.getString(smCursor.getColumnIndex("_id")));
results.add(smCursor.getString(smCursor.getColumnIndex("_id")));
}
}
if(results != null && !results.isEmpty()){
String [] mSelectionArgs = results.toArray(new String[results.size()]);
String mSelectionClause = "_id=?";
int delCursor = 0;
Uri uri = Uri.parse("content://contact");
for(int delCount = 0;delCount < mSelectionArgs.length;delCount++)
{
delCursor = getContentResolver().delete(uri ,mSelectionClause,new String[]{mSelectionArgs[delCount]});
}
ada.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), mSelectionArgs.length +" number of rows deleted", 5).show();
}
else
Toast.makeText(getApplicationContext(), "Sorry no deletion occurs", 5).show();
}
我想知道如何更新我的列表视图,因为我从列表视图中删除所选项目,删除后选择自动发生在下一个项目。我使用了notifyDataSetChanged()但不起作用。我正在使用自定义适配器。
答案 0 :(得分:0)
要在删除后使其工作,您需要在调用notifyDataSetChanged方法之前更改数据源,这可以通过smCursor.requery()实现;方法,但作为其弃用的方法,我建议再次执行查询,所以单击删除按钮时的代码块应该是:
btnDel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ArrayList<Boolean> ca = ada.getItemsThatAreChecked();
// this ArrayList will hold our Strings from the rows that were clicked.
ArrayList<String> results = new ArrayList<String>();
int s = ca.size();
for (int i = 0; i < s; i++) {
if (ca.get(i).booleanValue()) {
smCursor.moveToPosition(i);
Log.v("sss", smCursor.getString(smCursor.getColumnIndex("_id")));
results.add(smCursor.getString(smCursor.getColumnIndex("_id")));
}
}
if(results != null && !results.isEmpty()){
String [] mSelectionArgs = results.toArray(new String[results.size()]);
String mSelectionClause = "_id=?";
int delCursor = 0;
Uri uri = Uri.parse("content://contact");
for(int delCount = 0;delCount < mSelectionArgs.length;delCount++)
{
delCursor = getContentResolver().delete(uri ,mSelectionClause,new String[]{mSelectionArgs[delCount]});
}
smCursor = getContentResolver().query(
Uri.parse("content://contact"),
new String[] { "_id", "address", "body" }, "address like?",
new String[] { searchContactPhone }, null);
ada.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), mSelectionArgs.length +" number of rows deleted", 5).show();
}
else
Toast.makeText(getApplicationContext(), "Sorry no deletion occurs", 5).show();
}
答案 1 :(得分:0)
btnDel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ArrayList<Boolean> ca = ada.getItemsThatAreChecked();
// this ArrayList will hold our Strings from the rows that were clicked.
ArrayList<String> results = new ArrayList<String>();
int s = ca.size();
for (int i = 0; i < s; i++) {
if (ca.get(i).booleanValue()) {
smCursor.moveToPosition(i);
Log.v("sss", smCursor.getString(smCursor.getColumnIndex("_id")));
results.add(smCursor.getString(smCursor.getColumnIndex("_id")));
}
}
if(results != null && !results.isEmpty()){
String [] mSelectionArgs = results.toArray(new String[results.size()]);
String mSelectionClause = "_id=?";
int delCursor = 0;
Uri uri = Uri.parse("content://contact");
for(int delCount = 0;delCount < mSelectionArgs.length;delCount++)
{
delCursor = getContentResolver().delete(uri ,mSelectionClause,new String[]{mSelectionArgs[delCount]});
}
ada = new CustomAdapter(this,
R.layout.sm_layout, smCursor, new String[] {
"body" }, new int[] {
R.id.txt2 });
lvSm.setAdapter(ada);
Toast.makeText(getApplicationContext(), mSelectionArgs.length +" number of rows deleted", 5).show();
}
else
Toast.makeText(getApplicationContext(), "Sorry no deletion occurs", 5).show();
}
工作正常,因为我只重新创建自定义适配器的实例,并在删除后将其绑定到列表视图。