checkbox not get checked according to list values in android

夙愿已清 提交于 2020-01-22 02:49:25

问题


Sorry for my bad English,
I am individually getting array list value to the checkboxes. (Actually, these are the weekdays that are coming from the activity). but the problem is checkbox only set the first position value to checkbox. Please let me know what's wrong in my code.

Below is the Code Snippet

Let Say this is the list value:

[Monday, Tuesday, Sunday]

This is the class that i passing the list

for (int i = 0; i < myList.size(); i++) {

            switch (myList.get(i)){

                case "Monday":
                    cbMon.setChecked(true);
                    break;

                case "Tuesday" :
                    cbTue.setChecked(true);
                    break;

                case "Wednesday":
                    cbWed.setChecked(true);
                    break;

                case "Thursday":
                    cbThurs.setChecked(true);
                    break;

                case "Friday":
                    cbFri.setChecked(true);
                    break;

                case "Saturday":
                    cbSat.setChecked(true);
                    break;

                case "Sunday":
                    cbSun.setChecked(true);
                    break;

                default:

                    break;

            }

回答1:


use this structure no need to do it in hardcode way

for (int i = 0; i < myList.size(); i++) {

        switch (myList.get(i)){

            case "Monday":
                cbMon.setChecked(true);
                break;

            case "Tuesday" :
                cbTue.setChecked(true);
                break;

            case "Sunday":
                cbSun.setChecked(true);
                break;

            default:break;

        }
    }



回答2:


You don't need

for (int i = 0; i < myList.size(); i++)




回答3:


Try using OR (||) in your if() statement.

if(a==b  || a==c || b==c)
        {
            //Your action
        }

or Better use switch cases,

switch(position) {
  case 0:
    ...
    break;
  case 1:
    ...
    break;
  default:
    ...

}


来源:https://stackoverflow.com/questions/57985725/checkbox-not-get-checked-according-to-list-values-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!