简体   繁体   中英

Why setOnChangeListener don't work properly?

I have three switches with setOnChangeListener. The first two are working properly. But the third for some reasons is doing nothing. I read over my code like 50 times but I cant find whats wrong. It should save an int to SharedPreferences and show the Toast. The debuguer shows, that he goes into the case but do not execute it. After this is executed the MainActivity should load saved value and change the layout. But the code is even not saving. When i restart the SettingsTheme Activity the state of the switches should be loaded and that works for the first two but not for Swtich called rows.

It would be great if you could help!

I tried also to put the to do stuff from the third Listener to the second but it doesn't work. Tried to delete the save stuff in the onChangeListener and make the toast, tried to delete the variabels stuff in the MainActivity but nothing worked.

public class SettingsTheme extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_theme);
    setTitle("Themen");
    final Switch darkmode=findViewById(R.id.darkmode);
    final Switch auto_darkmode=findViewById(R.id.auto_darkmode);
    final Switch rows=findViewById(R.id.modul_rows);
    if(variables.LoadInt("dark_mode")==1){darkmode.setChecked(true);}
    if(variables.LoadInt("dark_mode")==2){
        auto_darkmode.setChecked(true);
        darkmode.setBackgroundResource(R.color.light_grey);
        darkmode.setClickable(false);
        darkmode.setVisibility(View.GONE);}
    if(variables.LoadInt("rows")==3){rows.setChecked(true);}

    darkmode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked==true){
                variables.SaveInt("dark_mode",1);
            }
            else{
                variables.SaveInt("dark_mode",0);
            }
        }
    });
    auto_darkmode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked==true){
                variables.SaveInt("dark_mode",2);
                darkmode.setBackgroundResource(R.color.light_grey);
                darkmode.setClickable(false);
                darkmode.setChecked(false);
                darkmode.setVisibility(View.GONE);
            }
            else{
                variables.SaveInt("dark_mode",0);
                darkmode.setClickable(true);
                darkmode.setBackgroundResource(0);
                darkmode.setVisibility(View.VISIBLE);
            }
        }
    });
    rows.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked==true){

                variables.SaveInt("rows",3);
                Toast.makeText(SettingsTheme.this, "3x7 Layout gewählt",Toast.LENGTH_SHORT);
            }
            else{
                variables.SaveInt("rows",2);
                Toast.makeText(SettingsTheme.this,"2x3 Layout,Toast.LENGTH_SHORT); }
        }
    });}




public class variables {

public static SharedPreferences Var;
public static int LoadInt(String s){
    Var=MainActivity.PublicContext.getSharedPreferences(s,       Context.MODE_PRIVATE);
    return Var.getInt(s,0);
}
public static void SaveInt(String s, int i){
    SharedPreferences.Editor editor=Var.edit();
    editor.putInt(s,i).apply();
}
public static String LoadString(String s){
    Var=MainActivity.PublicContext.getSharedPreferences(s, Context.MODE_PRIVATE);
    return Var.getString(s,"");
}
public static void SaveString(String s, String sts){
    SharedPreferences.Editor editor=Var.edit();
    editor.putString(s,sts).apply();
}

}

public class MainActivity extends AppCompatActivity {

public View root;
public static Context PublicContext=null;
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(PublicContext==null)PublicContext=getApplicationContext();
    View view1=getLayoutInflater().inflate(R.layout.activity_main_row2,null);
    View view2= getLayoutInflater().inflate(R.layout.activity_main_row3,null);
    if(variables.LoadInt("rows")==3){setContentView(view2);}
    else{setContentView(view1);}
}
}

Your Toast s are missing .show() . Hence no toasts are being shown, even if the code executes.

I solved this problem with changing code in variables.java. I used TinyDB class( https://github.com/kcochibili/TinyDB--Android-Shared-Preferences-Turbo/blob/master/TinyDB.java ):

public class variables {

public static TinyDB Var=new TinyDB(MainActivity.PublicContext);

public static  int LoadInt(String s){
    return Var.getInt(s);
}
public static void SaveInt(String s, int i){
    Var.putInt(s,i);
}
public static String LoadString(String s){
    return Var.getString(s);
}
public static void SaveString(String s, String sts){
    Var.putString(s,sts);
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM