开发者

ListView Checkboxes not updating properly

I have a simple adapter with an overridden getView method that is supposed to manually update the CheckBoxes to prevent the unwanted random checking that results from view recycling. So I put setChecked(boolean) for each checkbox, though it still doesn't update correctly. I have used Log stat开发者_高级运维ements to see what the booleans are, and the checkboxes are not the same as the boolean values. Does anyone have any suggestions?

Thanks!

Code here:

http://pastebin.com/MwgUc7Z8

    adapter = new SimpleAdapter(this, list, R.layout.rowsecond,
            new String[] { "icon", "name", "checkbox" }, new int[] {
                    R.id.image1, R.id.text1, R.id.checkbox }) {
        public View getView(int position, View convertView, ViewGroup parent) {

        //  View v = super.getView(position, convertView, parent);

            View v = convertView;
            if (v == null)
            {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.rowsecond, null);
            }

            ImageView imgV = (ImageView)v.findViewById(R.id.image1);

            imgV.setImageDrawable(getResources().getDrawable(((Integer)list.get(position).get("icon")).intValue()));

            TextView tv = (TextView) v.findViewById(R.id.text1);

            tv.setText((String)list.get(position).get("name"));

            list.get(position).get("icon");




            boxer = (CheckBox) v.findViewById(R.id.checkbox);
            //

            boxer.setTag(new Integer(position));


            switch(position)
            {
            case 0: 
                boxer.setChecked(CategoriesFirst.fr_one);
                break;
            case 1:
                boxer.setChecked(CategoriesFirst.fr_two);
                break;
            case 2:
                boxer.setChecked(CategoriesFirst.fr_three);
                break;
            case 3:
                boxer.setChecked(CategoriesFirst.fr_four);
                break;
            case 4:
                boxer.setChecked(CategoriesFirst.fr_five);
                break;
            case 5:
                boxer.setChecked(CategoriesFirst.fr_six);
                break;
            case 6:
                boxer.setChecked(CategoriesFirst.fr_seven);
                break;
            case 7:
                boxer.setChecked(CategoriesFirst.fr_eight);
                break;
            case 8:
                boxer.setChecked(CategoriesFirst.fr_nine);
                break;
            case 9:
                boxer.setChecked(CategoriesFirst.fr_ten);
                break;
            case 10:
                boxer.setChecked(CategoriesFirst.fr_eleven);
                break;
            case 11:
                boxer.setChecked(CategoriesFirst.fr_twelve);
                break;
            case 12:
                boxer.setChecked(CategoriesFirst.fr_thirteen);
                break;
            case 13:
                boxer.setChecked(CategoriesFirst.fr_fourteen);
                break;




            }




            boxer.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    Integer posSelected = (Integer) buttonView.getTag();

                    int posSelectedint = posSelected.intValue();

                    switch (posSelectedint) {
                    // set static booleans
                    // REMEMBER BREAKS!
                    case 0:
                        CategoriesFirst.fr_one = !CategoriesFirst.fr_one;
                        break;
                    case 1:
                        CategoriesFirst.fr_two = !CategoriesFirst.fr_two;
                        break;
                    case 2:
                        CategoriesFirst.fr_three = !CategoriesFirst.fr_three;
                        break;
                    case 3:
                        CategoriesFirst.fr_four = !CategoriesFirst.fr_four;
                        break;
                    case 4:
                        CategoriesFirst.fr_five = !CategoriesFirst.fr_five;
                        break;
                    case 5:
                        CategoriesFirst.fr_six = !CategoriesFirst.fr_six;
                        break;
                    case 6:
                        CategoriesFirst.fr_seven = !CategoriesFirst.fr_seven;
                        break;
                    case 7:
                        CategoriesFirst.fr_eight = !CategoriesFirst.fr_eight;
                        break;
                    case 8:
                        CategoriesFirst.fr_nine = !CategoriesFirst.fr_nine;
                        break;
                    case 9:
                        CategoriesFirst.fr_ten = !CategoriesFirst.fr_ten;
                        break;
                    case 10:
                        CategoriesFirst.fr_eleven = !CategoriesFirst.fr_eleven;
                        break;
                    case 11:
                        CategoriesFirst.fr_twelve = !CategoriesFirst.fr_twelve;
                        break;
                    case 12:
                        CategoriesFirst.fr_thirteen = !CategoriesFirst.fr_thirteen;
                        break;
                    case 13:
                        CategoriesFirst.fr_fourteen = !CategoriesFirst.fr_fourteen;
                        break;
                    default:
                        break;
                    }



                    CategoriesFirst.save("fr_one",
                            CategoriesFirst.fr_one);
                    CategoriesFirst.save("fr_two",
                            CategoriesFirst.fr_two);
                    CategoriesFirst.save("fr_three",
                            CategoriesFirst.fr_three);
                    CategoriesFirst.save("fr_four",
                            CategoriesFirst.fr_four);
                    CategoriesFirst.save("fr_five",
                            CategoriesFirst.fr_five);
                    CategoriesFirst.save("fr_six",
                            CategoriesFirst.fr_six);
                    CategoriesFirst.save("fr_seven", CategoriesFirst.fr_seven);
                    CategoriesFirst.save("fr_eight", CategoriesFirst.fr_eight);
                    CategoriesFirst.save("fr_nine", CategoriesFirst.fr_nine);
                    CategoriesFirst.save("fr_ten", CategoriesFirst.fr_ten);

                    CategoriesFirst.save("fr_eleven", CategoriesFirst.fr_eleven);
                    CategoriesFirst.save("fr_twelve", CategoriesFirst.fr_twelve);
                    CategoriesFirst.save("fr_thirteen", CategoriesFirst.fr_thirteen);
                    CategoriesFirst.save("fr_fourteen", CategoriesFirst.fr_fourteen);
                }
            });


            return v;
        }

    };


Did you try setting boxer.setChecked(false) before the switch clause? Because that would reset all the checkboxes.


It was quite long ago, but I might help somebody that is in the same situation. I solved the problem notifying the data set changed in the adapter. expandablelistview.notifyDataSetChanged(); After setting this, the UI was updating after each choice made in the list even after checking the state using isChecked();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜