Android Custom Dialog Multi-Select dialog box (dialog+listview+checkbox)

Source: Internet
Author: User

First put effect
The project needs to have a dialog Select All dialog box, click All selected, deselect all to cancel all. In the afternoon, I looked up some information and rewrote the dialog dialog box. Put the code out.

public class Mainactivity extends Activity {View getlistview;    String[] Mlisttext = {"Select All", "Choose 1", "Select 2", "SELECT 3", "Select 4", "Select 5", "Select 6", "Select 7"};    arraylist<map<string, object>> mdata = new arraylist<map<string, object>> ();    Alertdialog.builder Builder;    Alertdialog Builder2;    Simpleadapter adapter;    boolean[] bl = {false, False, False, False, False, False, false};        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Button button = (button) Findviewbyid (R.ID.BTN);        int lengh = Mlisttext.length;            for (int i = 0; i < Lengh; i++) {map<string, object> item = new hashmap<string, object> ();            Item.put ("text", Mlisttext[i]);        Mdata.add (item);           } button.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {     Createdialog ();//click Create Dialog}}); } class Itemonclick implements Onitemclicklistener {@Override public void Onitemclick (ADAPTERVIEW&LT;?&G T            arg0, view view, int position, long id) {checkbox cBox = (checkbox) View.findviewbyid (R.id.x_checkbox);            if (cbox.ischecked ()) {cbox.setchecked (false);                } else {log.i ("TAG", "Cancel this option");            Cbox.setchecked (TRUE); } if (position = = 0 && (cbox.ischecked ())) {//If you select All selected, select All and then update fo                R (int i = 0; i < bl.length; i++) {bl[i] = true;            } adapter.notifydatasetchanged (); } else if (position = = 0 && (!cbox.ischecked ())) {//If you cancel the Select all, cancel all and then update the for (int i = 0; i < bl.length;                i++) {Bl[i] = false; } adapter.notifydatasetchanged();  } if (Position! = 0 && (!cbox.ischecked ())) {//If the other option is canceled, cancel the Select all Bl[0]                = false;                Bl[position]=false;            Adapter.notifydatasetchanged (); } else if (position! = 0 && (cbox.ischecked ())) {//If you choose another option, see if Select All//First select the option set to T                Rue Bl[position]=true;                int a = 0;  for (int i = 1; i < bl.length; i++) {if (bl[i] = = False) {//If one is unchecked, it is not a full selection                    Jump directly out of the loop break;                        } else {//calculation of how many selected a++;                            if (a = = Bl.length-1) {//If the option is selected, select All selected, then update bl[0] = true;                        Adapter.notifydatasetchanged (); }}}}}} public void CreaTedialog () {///dynamically load a ListView layout file in Layoutinflater inflater = Layoutinflater.from (mainactivity.this);        Getlistview = inflater.inflate (R.layout.listview, NULL);        Bind content to ListView ListView ListView = (ListView) Getlistview.findviewbyid (R.id.x_listview); adapter = new Setsimpleadapter (Mainactivity.this, Mdata, R.layout.listitem, new string[] {"Text"}, new in        T[] {r.id.x_item_text});        Add Adapter Listview.setadapter (adapter) to the ListView;        Listview.setitemscanfocus (FALSE);        Listview.setchoicemode (listview.choice_mode_multiple);        Listview.setonitemclicklistener (New Itemonclick ());        Builder = new Alertdialog.builder (this);        Builder.settitle ("Please select a query type");        Builder.seticon (R.drawable.ic_launcher);        Set the loaded ListView Builder.setview (Getlistview);        Builder.setpositivebutton ("OK", New Dialogonclick ());        Builder.setnegativebutton ("Cancel", New Dialogonclick ()); Builder.creaTe (). Show (); } class Dialogonclick implements Dialoginterface.onclicklistener {@Override public void OnClick (Dialogin                Terface dialog, int which) {switch (which) {case dialog.button_positive://OK button event            Break            Case dialog.button_negative://Cancel the event of the button break;            Default:break; }}}//Override Simpleadapterd GetView method class Setsimpleadapter extends Simpleadapter {public Setsimplea  Dapter (context context, list<? extends Map<string,?>> data, int resource, string[] from, int[]        To) {Super (context, data, resource, from, to); } @Override public View getView (int position, view Convertview, ViewGroup parent) {if (CONVERTVI            EW = = NULL) {Convertview = Linearlayout.inflate (Getbasecontext (), r.layout.listitem, NULL); } CheckBox Ckbox = (CheckBox) Convertview.findviewbyid (R.id.x_checkbox);            Update the checkbox if (bl[position] = = True) {ckbox.setchecked (true), each time according to bl[];            } else if (bl[position] = = False) {ckbox.setchecked (false);        } return Super.getview (position, convertview, parent); }    }}

Layout file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="${relativePackage}.${activityClass}" >    <Button        android:id="@+id/btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="点击弹出Dialog" /></RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="${relativePackage}.${activityClass}" >    <ListView        android:id="@+id/X_listview"        android:layout_width="match_parent"        android:layout_height="wrap_content" >    </ListView></RelativeLayout>
  <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http/ Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context = "${relativepackage}.${activityclass}" > <relativelayout android:layout_width= "wrap_content" Androi            d:layout_height= "Wrap_content" android:descendantfocusability= "blocksdescendants" > <TextView Android:id= "@+id/x_item_text" android:layout_width= "wrap_content" android:layout_height= "Wrap_con Tent "android:text=" option "/> <checkbox android:id=" @+id/x_checkbox "Android:la Yout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentright= "true "Android:clickable=" false "android:focusable=" false "/> </relativelayout></linearlay Out>  

Android Custom Dialog Multi-Select dialog box (dialog+listview+checkbox)

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.