How to delete check box items from list

后端 未结 6 1231
花落未央
花落未央 2021-01-14 02:24

I have 50 list item in the list. Now i have checked 10 item, so how to this 10 checked item delete(remove) from the listview when I click delete button.

Here is My C

6条回答
  •  天涯浪人
    2021-01-14 02:52

    I performed the same task in one of my app.

    What you should do is use an ArrayList having the same size as the list item and each item contains 0 by default . Now in your Efficient Adapter for each checkbox that is checked , assign 1 in that particular position in arrayList . Finally on the button click of delete , traverse through the ArrayList and delete all those item from list which contain 1 in that postion in that ArrayList and atLast call notifyDatasetChanged() method of list so that list get refreshed and shows the new list.

    Here is some sample code to give you a better idea over this :

     ArrayList checks=new ArrayList();
    

    Now in onCreate method

    for(int b=0;b

    Now in your efficientAdapter's getView method

     public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.bookmarks_list_item,
                        null);
                holder = new ViewHolder();
                holder.text1 = (TextView) convertView
                        .findViewById(R.id.title);
                holder.text2 = (TextView) convertView
                        .findViewById(R.id.body);
                 holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
    
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
    
               holder.checkBox.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
                    // TODO Auto-generated method stub
    
                    if(((CheckBox)v).isChecked()){
                      checks.set(position, 1);
                    }
                    else{
                     checks.set(position, 0);
                    }
    
                }
            });
    
    
            return convertView;
        }
    

    Finally on delete Button click :

         delete.setOnClickListener(new OnClickListener() {
    
        public void onClick(View v) {
        // TODO Auto-generated method stub
    
              for(int i=0;i

    Hope this will solve your issue.

提交回复
热议问题