0
  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() 但不起作用。我正在使用自定义适配器。

4

2 回答 2

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();


        }
于 2012-05-14T04:41:00.057 回答
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();


        }

工作正常,因为我只重新创建自定义适配器的实例并在删除后将其绑定到列表视图。

于 2012-05-14T14:47:50.047 回答