2

I am getting listview in my layout... I don't know how can I delete multiple items from listview when I select items in checkbox and click delete button. I am adding a piece of code.

public class classname extends Activity implements OnItemSelectedListener {

 ListView lv;
 ArrayList<x_obj> x_list = new ArrayList<x_obj>();
 TextView  delete;
 @Override
     protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_x);
      del_txt = (TextView) findViewById(R.id.del_txt);

del_txt.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
           "code for delete selected items"

        }
    });
 }
public OrderAdapter(Context context, int row_layout,
            ArrayList<x_obj> items) {
        super(context, row_layout, items);
        this.items = items;
    }

    @Override
    public View getView(final int position, View convertview, ViewGroup parent) {
        View v = convertview;

        LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.row_layout, null);
        final x obj = items.get(position);

 TextView txt_name = (TextView) v.findViewById(R.id.txt_name);
  CheckBox checkBox1=(CheckBox)findViewById(R.id.checkBox1);

  return v;
    }
}
3
  • so are you displaying checkboxes beside listitem? in this case,you can loop your arraylist and check whether a item is checked or not.if checked then add it to a different arraylist and replace old arraylist with new arraylist and call notifydatasetchanged. Commented Nov 14, 2015 at 8:24
  • now i am getting checkbox in listview..i didnt do code part in checkbox..can u help..i added my code here Commented Nov 14, 2015 at 8:25
  • i am getting list items from web respons..and add that items in x object...doing that part in back ground using asynch task Commented Nov 14, 2015 at 8:31

2 Answers 2

1
public class classname extends Activity implements OnItemSelectedListener {

    ListView lv;
     ArrayList<x_obj> new_al=new ArrayList<x_obj>();
     ArrayList<x_obj> x_list = new ArrayList<x_obj>();
      TextView  delete;
   @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_x);
  del_txt = (TextView) findViewById(R.id.del_txt);

   del_txt.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
       "code for delete selected items"

    }
});
        }
          public OrderAdapter(Context context, int row_layout,
        ArrayList<x_obj> items) {
    super(context, row_layout, items);
    this.items = items;
}

@Override
public View getView(final int position, View convertview, ViewGroup parent) {
    View v = convertview;

    LayoutInflater vi = (LayoutInflater)                    
           getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = vi.inflate(R.layout.row_layout, null);
    final x obj = items.get(position);

      TextView txt_name = (TextView) v.findViewById(R.id.txt_name);
         CheckBox checkBox1=(CheckBox)findViewById(R.id.checkBox1);
             if(checkBox1.isChecked())
           {
           new_al.add(x_list.get(position));//add the element into arraylist

                }  
                 btn_clear.setOnItemClickListener(new Listener()
              {

            public void onClick(View v)
             {

             x_list.clear();
             x_list.add(new_al);
           }
            }
               );
              notifyDataSetChanged();
            return v;

              }
                  }

This is just an exmple.there can be syntactical errors at some places.please check them.

Sign up to request clarification or add additional context in comments.

2 Comments

i think all you need to do is to add a button to clear all the checked items in the list
ia m using delete text for clear list..del_txt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { "code for delete selected items" } });
1

try this in your adapter, i used it by extending BaseAdapter:

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

final ViewHolder holder;

   if (convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.row1, null);

        holder.checkbox = (CheckBox) convertView
                .findViewById(R.id.add_food_item);
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    final ItemInList element = list.get(position);

    holder.checkbox.setChecked(element.isSelected());
    holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    element.setSelected(buttonView.isChecked());

                    if (element.isSelected()) {
                        array.add(element.getName());
                    } else {
                        if (position < array.size())
                            array.remove(position);
                    }
                }
            });

    return convertView;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.