Android uses ListView to bulk Delete Item methods _android

Source: Internet
Author: User
Tags sqlite database

The example in this article describes how Android uses ListView to bulk Delete item. Share to everyone for your reference, specific as follows:

Use the checkbox to select one or more item, and then delete them in bulk.

The program operation effect chart is as follows:

Start the code below:

(The code has been corrected, and the problem of rolling disturbances is resolved)

Package com.test.adapter;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import android.app.ListActivity;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.BaseAdapter;
Import Android.widget.CheckBox;
Import Android.widget.ListView;
Import Android.widget.TextView;
Import Android.widget.Toast;
Import Com.test.adapter.ArrayAdapterActivity.ListVIewAdapter.ViewHolder; /** * ListView Bulk Delete Item * @author MANYMORE13 * * */public class Arrayadapteractivity extends Listactivity {private L
  Istviewadapter Ladapter;
  Private list<string> strlist = new arraylist<string> ();
  Private list<boolean> boollist = new arraylist<boolean> (); Boolean visflag = fAlse;
  ListView LV; Static String str[] ={"1 Apples", "2 Bananas", "3 Oranges", "4 Kiwi", "5 Plums", "6 Sugarcane", "7 Litchi", "8 Peaches", "9 cantaloupe", "10 Grapes", "11 cantaloupe", "12 Longan", "13 Longan", "14", "
  15 "," 16 "," 17 "," 18 "," 19 "," 20 "};
  CheckBox CB;
      {for (int i=0;i<str.length;i++) {strlist.add (str[i]);
    Boollist.add (FALSE);
    } @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Ladapter = new Listviewadapter (this);
    LV = This.getlistview ();
    Lv.setadapter (Ladapter);
    Lv.setscrollbarstyle (1); Lv.setonitemclicklistener (New Onitemclicklistener () {@Override public void Onitemclick (ADAPTERVIEW&LT;?&G T Parent, view view, int position, long id) {if (Visflag) {Viewholder Viewholder
          = (Viewholder) view.gettag ();
          ViewHolder.cb.toggle ();
          if (viewHolder.cb.isChecked ()) {Boollist.set (position, true);
 }else{Boollist.set (position, false);         }
        }
      }
    });
    Class Listviewadapter extends Baseadapter {context C;
    Layoutinflater Minflater;
      Listviewadapter (Context context) {c = context;
    Minflater = Getlayoutinflater ();
    @Override public int GetCount () {return strlist.size ();
    @Override public Object getitem (int position) {return strlist.get (position);
    @Override public long getitemid (int position) {return position; @Override Public View getview (final int position, View Convertview, ViewGroup parent) {viewho
      Lder holder = null;
        if (Convertview = = null) {holder = new Viewholder ();
        Convertview = minflater.inflate (R.layout.list_item, NULL);
        Holder.tv = (TextView) Convertview.findviewbyid (R.id.textview);
        HOLDER.CB = (CheckBox) Convertview.findviewbyid (R.id.checkbox);
      Convertview.settag (holder); }else{Holder = (Viewholder) convertview.gettag ();
      } holder.tv.setText (Strlist.get (position));
      Holder.cb.setChecked (Boollist.get (position));
      if (Visflag) {holder.cb.setVisibility (view.visible);
      else {holder.cb.setVisibility (view.invisible);
    return convertview;
      Class Viewholder {TextView TV;
    CheckBox CB;
    @Override public boolean Oncreateoptionsmenu (Menu menu) {menu.add (0, 0, 0, batch processing);
    Menu.add (0, 1, 0, "confirm deletion");
  return Super.oncreateoptionsmenu (menu); @Override public boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {case 0:/
            /Bulk Processing {if (Visflag) {Visflag = false;
            for (int i=0; i<boollist.size (); i++) {Boollist.set (I, false);
          } else {Visflag = true; } THIS.LADAPTer.notifydatasetinvalidated ();
        Break
              Case 1://Determine delete {if (boollist.size () >0) {if (Visflag) {
              for (int location=0; location<boollist.size ();)
                  {if (Boollist.get (location)) {Boollist.remove (location);
                  Strlist.remove (location);
                Continue
              } location++;
          }} this.ladapter.notifyDataSetChanged ();
        Break
  } return super.onoptionsitemselected (item);

 }
}

and List_item.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" "Fill_parent"
  ;
      <relativelayout android:layout_width= "fill_parent" android:layout_height= "wrap_content" > <TextView
      Android:id= "@+id/textview" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"
      android:text= "@string/hello" android:layout_alignparentleft= "true" android:layout_centervertical= "true" /> <checkbox android:id= "@+id/checkbox" android:layout_width= "Wrap_content" Android:layout_hei ght= "Wrap_content" android:layout_alignparentright= "true" android:visibility= "Gone" android:focusable= "F" Alse "android:focusableintouchmode= false" android:clickable= "false"/> </RelativeLayout>

 ;/linearlayout>

For more information on Android-related content readers can view the site: "The activity of Android programming skills Summary", "Android Resources Operating Skills Summary", "Android File Operating skills summary", " Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", The Android View view tips summary and the Android Control usage summary

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.