In my app I am trying to store checkbox value in shared preference,when user click on btn checkbox value should store in shared preference..But whenever again i open my app checkbox status got null,following is my snippet code,what is the issue with this code..
public class ConfigurationsActivity extends Activity {
ImageView img_navigation_link;
private Button btn_set_configs;
String who;
private EditText edt_api_url, edt_username, edt_password;
private CheckBox edt_Autenticate;
private Button btn_Save_settings;
private TextView txtuname;
private TextView txtpname;
private TextView txtauth;
int aj;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_configuaration);
edt_api_url = (EditText) findViewById(R.id.edt_api_url);
initialize();
onclickWidgets();
if(consts.pref.getBoolean("checkbox", false))
{
edt_Autenticate.setChecked(true);
}
else
{
edt_Autenticate.setChecked(false);
}
edt_Autenticate.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
aj=1;
System.out.println("when aj 1"+aj);
//consts.pref = getSharedPreferences("pref", MODE_PRIVATE);
consts.pref.edit().putBoolean("check", true).commit();
}
else
{
aj=2;
System.out.println("when aj 2"+aj);
}
}
});
// imgone=(RelativeLayout)findViewById();
}
private void initialize() {
consts.pref = getSharedPreferences("pref", MODE_PRIVATE);
consts.editor = consts.pref.edit();
who = consts.pref.getString("who", "");
consts.Base_URL = consts.pref.getString(consts.Base_URL, consts.Base_URL);
edt_api_url.setText(consts.Base_URL);
TextView current_office_name = (TextView) findViewById(R.id.txt_configuration);
current_office_name.setText(consts.pref.getString("office_name", ""));
//
Log.d("api url configurations", consts.pref.getString(consts.Base_URL, consts.Base_URL));
txtuname = (TextView) findViewById(R.id.txt_username);
txtpname = (TextView) findViewById(R.id.txt_password);
txtauth = (TextView) findViewById(R.id.txt_Autenticate);
edt_username = (EditText) findViewById(R.id.edt_username);
edt_password = (EditText) findViewById(R.id.edt_password);
edt_Autenticate = (CheckBox) findViewById(R.id.edt_Autenticate);
btn_Save_settings = (Button) findViewById(R.id.btn_Save_settings);
// System.out.println(" check nu status "+consts.pref.getBoolean("checkbox", false));
if (who.equals("config")) {
edt_username.setVisibility(View.GONE);
edt_password.setVisibility(View.GONE);
edt_Autenticate.setVisibility(View.GONE);
txtpname.setVisibility(View.GONE);
txtuname.setVisibility(View.GONE);
txtauth.setVisibility(View.GONE);
} else if (who.equals("user")) {
edt_api_url.setKeyListener(null);
}
img_navigation_link = (ImageView) findViewById(R.id.img_navigation_link);
img_navigation_link.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(ConfigurationsActivity.this, MainMenu.class);
startActivity(intent);
ConfigurationsActivity.this.finish();
}
});
btn_Save_settings.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method s
if (who.equals("config")) {
if (edt_api_url.getText().toString().trim().equals("")) {
Toast.makeText(getApplicationContext(), "Endereço API inválido", Toast.LENGTH_SHORT).show();
} else {
{
consts.editor.putString(consts.Base_URL, edt_api_url.getText().toString().trim());
consts.editor.commit();
Toast.makeText(getApplicationContext(), "As configurações foram guardadas",
Toast.LENGTH_SHORT).show();
}
}
} else if (who.equals("user")) {
if (edt_username.getText().toString().trim().equals("")
&& edt_password.getText().toString().trim().equals("")) {
Toast.makeText(getApplicationContext(), "Digite nome de usuário e senha", Toast.LENGTH_SHORT)
.show();
} else {
if (edt_username.getText().toString().trim().equals(consts.pref.getString("user_name", ""))
&& edt_password.getText().toString().trim()
.equals(consts.pref.getString("user_password", ""))) {
if(aj==1)
{
consts.editor.putBoolean("checkbox", true).commit();
consts.editor.commit();
}
else
{
consts.editor.putBoolean("checkbox", false).commit();
consts.editor.commit();
}
Toast.makeText(getApplicationContext(), "As configurações foram guardadas",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Nome de usuário e senha errada",
Toast.LENGTH_SHORT).show();
}
}
}
}
});
}
private void onclickWidgets() {
}
Aucun commentaire:
Enregistrer un commentaire