You need to programmatically change the isChecked property in dynamically created Switch elements. The fragment code in which the switches are created is as follows:
package com.cyberpole.cleverhome; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.RelativeLayout; import android.widget.Switch; import android.widget.TextView; public class LightingsFragment extends Fragment { TextView textMsg; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { // Динамическое создание элементов фрагмента Context context = getActivity().getApplicationContext(); RelativeLayout layout = new RelativeLayout(context); int wrapContent = RelativeLayout.LayoutParams.WRAP_CONTENT; int matchParent = RelativeLayout.LayoutParams.MATCH_PARENT; RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(wrapContent, matchParent); textMsg = new TextView(context); textMsg.setId(layout.getId()+500); textMsg.setTextSize(20); textMsg.setTextColor(Color.BLACK); layout.addView(textMsg, layoutParams); int previousTextViewId = textMsg.getId(); for (int i = 0; i < MainActivity.maxRoomCount; i++){ RelativeLayout.LayoutParams layoutParamsOfText = new RelativeLayout.LayoutParams(wrapContent, wrapContent); RelativeLayout.LayoutParams layoutParamsOfSwitch = new RelativeLayout.LayoutParams(wrapContent, wrapContent); // Добавляем текстовое поле слева layoutParamsOfText.addRule(RelativeLayout.ALIGN_PARENT_LEFT); layoutParamsOfText.topMargin = 60 + i*50; TextView roomName = new TextView(context); roomName.setId(1002 + i); previousTextViewId = roomName.getId(); roomName.setGravity(RelativeLayout.CENTER_VERTICAL); roomName.setText(MainActivity.configRooms[i]); roomName.setTextSize(20); roomName.setLayoutParams(layoutParamsOfText); layout.addView(roomName); // Добавляем переключатель Switch int alignParentRight = RelativeLayout.ALIGN_PARENT_RIGHT; layoutParamsOfSwitch.addRule(alignParentRight); layoutParamsOfSwitch.addRule(RelativeLayout.ALIGN_BASELINE, previousTextViewId); layoutParamsOfSwitch.addRule(RelativeLayout.ALIGN_RIGHT, previousTextViewId); Switch switch1 = new Switch(context); switch1.setId(2002 + MainActivity.configOutputs[0][i]); switch1.setTextColor(Color.BLACK); switch1.setLayoutParams(layoutParamsOfSwitch); layout.addView(switch1); } Bundle bundle = getArguments(); if (bundle != null) { String msg = bundle.getString(MainActivity.KEY_MSG_LIGHTINGS); if (msg != null) { textMsg.setText(msg); } } return layout; } // Вывод текста в заголовок фрагмента public void setMsg(String msg){ textMsg.setText(msg); } } Depending on the variable-changing value of some variable, it is necessary to change the states of the switches. When creating switches, I set them id programmatically, so the id of the necessary elements is obtained are known. But how is it possible after the display of elements on the screen to programmatically change the isChecked property isChecked not occur to me. Help me please.
switch1.setId(2002 + MainActivity.configOutputs[0][i]);. Perhaps the project will already have such an id. How correctly to generate dynamic ID look here at stackoverflow.com/a/15442898/1568530 . Secondly, you can save yourSwitchto variables, and not find them later on by ID, but directly work with the variable. - Vladyslav Matviienko