2017-05-22 181 views
-5

我在我的行中有一个Listview,我有复选框选择要删除的项目。 我想要一个按钮来选择我的所有项目并同时删除它们。 当我点击我的按钮来选择所有项目时,只有最后一个项目被选中。删除列表中的所有项目

这里是我的代码:

all.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       for(int i=0;i<listview.getChildCount();i++){ 
        Log.i("mhs",listview.getChildCount()+""); 
        checkBox.setChecked(true); 
       } 


      } 
     }); 

这是我Adaptor

public class CustomArrayAdapter extends ArrayAdapter<String> { 

    private LayoutInflater inflater; 

    // This would hold the database objects i.e. Blacklist 
    private List<Blacklist> records; 

    @SuppressWarnings("unchecked") 
    public CustomArrayAdapter(Context context, int resource, @SuppressWarnings("rawtypes") List objects) { 
     super(context, resource, objects); 

     this.records = objects; 
     inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 

     //Reuse the view to make the scroll effect smooth 
     if (convertView == null) 
      convertView = inflater.inflate(R.layout.list_item, parent, false); 

     // Fetch phone number from the database object 
     final Blacklist phoneNumber = records.get(position); 

     // Set to screen component to display results 
     ((TextView) convertView.findViewById(R.id.serial_tv)).setText("" + (position + 1)); 
     ((TextView) convertView.findViewById(R.id.phone_number_tv)).setText(phoneNumber.phoneNumber); 
     checkBox = (CheckBox) convertView.findViewById(R.id.check); 
     final LinearLayout me = (LinearLayout) convertView.findViewById(R.id.me); 
     if (position + 1 >= 1) { 
      me.setVisibility(View.VISIBLE); 

     } 
     checkBox.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       if (((CheckBox) v).isChecked()) { 



        final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this); 

        // Set the appropriate message into it. 
        alertDialogBuilder.setMessage(getResources().getString(R.string.block)); 

        // Add a positive button and it's action. In our case action would be deletion of the data 
        alertDialogBuilder.setPositiveButton(getResources().getString(R.string.yes), 
          new DialogInterface.OnClickListener() { 
           @Override 
           public void onClick(DialogInterface arg0, int arg1) { 
            try { 

             blackListDao.delete(blockList.get(position)); 

             // Removing the same from the List to remove from display as well 
             blockList.remove(position); 
             listview.invalidateViews(); 

             // Reset the value of selectedRecordPosition 
             selectedRecordPosition = -1; 
             populateNoRecordMsg(); 
            } catch (Exception e) { 
             e.printStackTrace(); 
            } 
           } 
          }); 

        // Add a negative button and it's action. In our case, just hide the dialog box 
        alertDialogBuilder.setNegativeButton(getResources().getString(R.string.no), 
          new DialogInterface.OnClickListener() { 

           @Override 
           public void onClick(DialogInterface dialog, int which) { 
            checkBox.setChecked(false); 
           } 
          }); 

        // Now, create the Dialog and show it. 
        final AlertDialog alertDialog = alertDialogBuilder.create(); 
        alertDialog.show(); 
       } 

      } 
     }); 
     all.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       for(int i=0;i<listview.getChildCount();i++){ 
        Log.i("mhs",listview.getChildCount()+""); 
        checkBox.setChecked(true); 
        listview.removeView(i); 
       } 


      } 
     }); 

     return convertView; 
    } 

} 

如何选择所有的物品放在一起?

+1

只需清除你的列表就像YOUR_ARRAYLIST.clear();并确保在清除清单后使用notifiyDatachange –

+0

发布您的适配器类也 –

+0

我假设您想从某种持久性系统数据库,文件中删除项目...您只需删除所有此项目,然后更新您的适配器 – Timo

回答

0

与列表视图的问题是视图被回收!这意味着致电

  checkBox.setChecked(true); 

只是简单地编辑当前活动项目。如果你想删除所有的项目,最好的办法是清除你正在使用的列表和notifyingDataSetChanged()。

+0

如何清除我的列表?如何删除我的所有复选框然后删除它们? – user7908469

+0

records.clear(); – fsebek

+0

你的意思是mylistview.clear?我不明白你的意思 – user7908469

0

在for循环补充一点:

currentCheckBox = listView.getChildAt(i); 
//now you should have the correct checkBox 

编辑:如果您的需求是简单地删除数据和结算,我会做只是为了一个方法。调用它,删除数据库中的项目,然后.clear()适配器。 (不记得notifyDatasetChanged()是否在这里自动调用,如果不是)