From cc532b9107f483bdb1c7e5dacfdaee39fbeb8c1c Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 3 May 2018 20:13:27 +0200 Subject: [PATCH] SWRadioButton cleanup --- .../startupwizard/SWRadioButton.java | 59 ++++++------------- 1 file changed, 17 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWRadioButton.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWRadioButton.java index 882f361a32..461dfa1298 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWRadioButton.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWRadioButton.java @@ -11,17 +11,15 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.events.EventPreferenceChange; -import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.startupwizard.events.EventSWUpdate; import info.nightscout.utils.SP; public class SWRadioButton extends SWItem { - private static Logger log = LoggerFactory.getLogger(SWRadioButton.class); + int labelsArray; int valuesArray; private RadioGroup radioGroup; - public boolean somethingChecked = false; public SWRadioButton() { super(Type.RADIOBUTTON); @@ -44,56 +42,33 @@ public class SWRadioButton extends SWItem { @Override public void generateDialog(View view, LinearLayout layout) { Context context = view.getContext(); - String[] labels = context.getResources().getStringArray(labelsArray); - String[] values = context.getResources().getStringArray(valuesArray); // Get if there is already value in SP String previousValue = SP.getString(preferenceId, "none"); -// log.debug("Value for "+view.getContext().getString(preferenceId)+" is "+previousValue); radioGroup = new RadioGroup(context); radioGroup.clearCheck(); + radioGroup.setOrientation(LinearLayout.VERTICAL); + radioGroup.setVisibility(View.VISIBLE); - for (int row = 0; row < 1; row++) { - - radioGroup.setOrientation(LinearLayout.VERTICAL); - radioGroup.setVisibility(View.VISIBLE); - - for (int i = 0; i < labels.length; i++) { - RadioButton rdbtn = new RadioButton(context); - rdbtn.setId((row * 2) + i); - rdbtn.setText(labels[i]); - if (previousValue.equals(values[i])) - rdbtn.setChecked(true); - radioGroup.addView(rdbtn); - } + for (int i = 0; i < labels().length; i++) { + RadioButton rdbtn = new RadioButton(context); + rdbtn.setId(View.generateViewId()); + rdbtn.setText(labels()[i]); + if (previousValue.equals(values()[i])) + rdbtn.setChecked(true); + rdbtn.setTag(i); + radioGroup.addView(rdbtn); } radioGroup.setOnCheckedChangeListener((group, checkedId) -> { - save(); + int i = (int) group.findViewById(checkedId).getTag(); + save(i); }); layout.addView(radioGroup); - } - public RadioGroup getRadioGroup() { - return this.radioGroup; + public void save(int i) { + SP.putString(preferenceId, values()[i]); + MainApp.bus().post(new EventPreferenceChange(preferenceId)); + MainApp.bus().post(new EventSWUpdate()); } - - public String getCheckedValue() { - if (radioGroup != null && radioGroup.getCheckedRadioButtonId() > -1) { - Context context = radioGroup.getRootView().getContext(); - String[] values = context.getResources().getStringArray(valuesArray); - return values[radioGroup.getCheckedRadioButtonId()]; - } else { - return "none"; - } - } - - public void save() { - if (!getCheckedValue().equals("none")) { - SP.putString(preferenceId, getCheckedValue()); - MainApp.bus().post(new EventPreferenceChange(preferenceId)); - MainApp.bus().post(new EventSWUpdate()); - } - } - }