Example Analysis of checkbox usage in ListView of Android programming _android

Source: Internet
Author: User

This example describes the use of checkbox in the ListView of Android programming. Share to everyone for your reference, specific as follows:

We often use a checkbox in ListView. Directly without the application will find that ListView in the Onitemclicklistener event and checkbox in the selection of events conflict, how to deal with it. directly on the code.

List_item.xml Code:

 <?xml version= "1.0" encoding= "Utf-8"?> <relativelayout "xmlns:android="
  /schemas.android.com/apk/res/android "android:layout_width=" match_parent "android:layout_height=" Match_parent " android:background= "@color/color_while" > <textview android:id= "@+id/txt_add_note_tag_list_name" Android:la yout_height= "50DP" android:layout_width= "fill_parent" android:gravity= "center_vertical" android:textColor= "@co Lor/color_black "android:layout_marginleft=" 8DP "/> <checkbox android:id=" @+id/chk_add_note_tag_list_c 
    HK "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:gravity=" center_vertical " Android:layout_alignparentright= "true" android:layout_marginright= "8DP" android:focusable= "false" <!--this Must be added, otherwise there will be a conflict--> android:clickable= "false" <!--this must be added, otherwise there will be conflicts-->/> </RelativeLayout>  

Baseadapter.java Code:

Package cg.guangda.androidnote;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import Cg.guangda.androidnote.Model.noteTag;
Import Android.content.Context;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.CheckBox;
Import Android.widget.TextView;
  public class Add_note_tag_list_baseadapter extends Baseadapter {private Layoutinflater inflater;
  Private list<notetag> list_notetag_date;
  Defines whether the multiple-selection box is checked for public static Map<integer, boolean> isselected; Public Add_note_tag_list_baseadapter (Context context,list<notetag> list_notetag_date) {this.inflater = Layout
    Inflater.from (context);
    This.list_notetag_date = list_notetag_date;
    Here defines isselected this map is a record of each ListItem state, the initial state is all false.
    isselected = new Hashmap<integer, boolean> (); for (int i = 0; i < list_notetag_date.size (); i++) {Isselected.put (I, false);
  @Override public int GetCount () {//TODO auto-generated Method stub return List_notetag_date.size (); @Override public Object getitem (int position) {//TODO auto-generated method stub return List_notetag_date
  . get (position);
  @Override public long getitemid (int position) {//TODO auto-generated method stub return position; @Override public View getview (int position, View Convertview, ViewGroup parent) {Add_note_notetag Notetag = nul
    L
      if (convertview==null) {Convertview = inflater.inflate (R.layout.add_note_tag_list_item, NULL);
      Notetag = new Add_note_notetag ();
      Notetag.txt_add_note_tag_list_name = (TextView) Convertview.findviewbyid (r.id.txt_add_note_tag_list_name);
      Notetag.chk_add_note_tag_list_chk = (CheckBox) Convertview.findviewbyid (R.ID.CHK_ADD_NOTE_TAG_LIST_CHK);
    Convertview.settag (Notetag); else {Notetag = (Add_note_notetag) CONvertview.gettag ();
    } notetag.txt_add_note_tag_list_name.setText (List_notetag_date.get (position). Get_tagname ());
    Notetag.chk_add_note_tag_list_chk.setChecked (Isselected.get (position));
  return convertview;
    public class Add_note_notetag {TextView txt_add_note_tag_list_name;
  CheckBox Chk_add_note_tag_list_chk;

 }
}

Application page:

List_popwin_note_tag.setadapter (This, list_notetag_date) (new add_note_tag_list_baseadapter);
List_popwin_note_tag.setonitemclicklistener (New Notetaglistitemonclicklistener ());

/**
* Click on list item Event
* @author CG
* * */
class Notetaglistitemonclicklistener implements onitemclicklistener{
    @Override public
    void Onitemclick (adapterview<?> arg0, view view, int position,
        long Arg3) {
      //TODO auto-generated method stub
      Add_note_notetag Vhollder = (add_note_notetag) view.gettag ();
      Changes the checkbox state for each item that is clicked, while modifying the value of the map.
      vHollder.chk_add_note_tag_list_chk.setChecked (vHollder.chk_add_note_tag_list_chk.isChecked () ==true? false : true);
      Boolean check = VHollder.chk_add_note_tag_list_chk.isChecked ();
      Add_note_tag_list_BaseAdapter.isSelected.put (position, check);
      LOG.V ("Notetaglistitemonclicklistener", list_notetag_date.get (position). Get_tagname () + check);
    }


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

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.