A problem with Android ListView

Source: Internet
Author: User
Tags final

The function you need is simple, a list with a checkbox, and you need to know what the user has chosen for the project, as shown in the following figure:

Using Simpleadapter as the data adapter, rewrite the Simpleadapter.viewbinder method so that it is easier to use than the custom adapter, the code below

Datas definition is private list<map<string, object>> datas=null;

Where a self-referencing (my) binding to a checkbox is kept in the map

Private map<string, object> populatemap (String lblno,
map<string, object> ... maps) {
    
map< String, object> map = null;
if (Maps.length > 0) {
map = maps[0];
} else {
map = new hashmap<string, object> ();
}
    
Map.put ("Lblno", Lblno);
Map.put ("My", map);
Map.put ("Checked", true);
return map;
}
private void Bindadapter () {int[] to = new int[] {r.id.lblno,r.id.ckbit};
        String[] from = new string[] {"Lblno", "I"};
        adapter = new Simpleadapter (this, datas, R.layout.activity_post_list_item, from, to); ======= Add Delete event =======//View this column more highlights: Http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/SimpleAdapter.View
                        
            Binder Binder = new Simpleadapter.viewbinder () {private Boolean supressevent=false; @Override public boolean setviewvalue (view view, Object data, String Tex
                    
                Trepresentation) {Final Object d = data;
                     if (view instanceof CheckBox) {final map map= (map) D;
                     CheckBox ckb = (checkbox) view;
                     LOG.D ("T", "->map" +map);
                     Ckb.settag (map); Supressevent=tRue
                     There is a need to avoid triggering the Oncheckedchange event listening processing ckb.setchecked ((Boolean) map.get ("checked"));
                         
                     Supressevent=false; Ckb.setoncheckedchangelistener (New Oncheckedchangelistener () {@Overr
                        
                            IDE public void OnCheckedChanged (Compoundbutton buttonview, Boolean ischecked) {
                            LOG.D ("T", "Oncheckedchanged->map" +map + "ischecked:" +ischecked);
                            if (supressevent) return;
                            Map map= (map) Buttonview.gettag ();
                                
                                
                    
                                
                        Map.put ("Checked", ischecked);
                        
    
                    }
                    }); return true; Returning TRUE indicates that no parent class is required for default settings//reference HTTP://www.cnblogs.com/carmanloneliness/p/3500832.html//http://www.cnblogs.com/carmanloneliness/p/3500832
            . html} return false;
        }
        };
    
        Adapter.setviewbinder (binder);
    =======end======= Lv.setadapter (adapter); }

At the beginning, without a red comment, it is found that the Run-time program behavior is not always required.

It was later discovered that CKB triggers the Oncheckedchange handler when executing setchecked.

Simpleadapter is also a control reuse mechanism, which is when the list is dragged up and down, the controls that are dragged out of the screen are reused (binding the new data, the link in the reference code), due to the final implementation of the ckb.setchecked ((Boolean) Map.get ("checked")); Triggers the control's Oncheckedchange handler, which points to the data item that was previously bound, that is, the previous invocation of the Setviewvalue incoming data, which could result in an unexpected modification of a data in datas, resulting in uncertainty about the behavior of the program.

The solution is to execute ckb. When setchecked, the event handler excludes the drag list from generating messages based on this tag.

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.