I have set the Preferences in xml and I have the folowing Activity:
public class Preferencias extends PreferenceActivity {
//SharedPreferences prefs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
setContentView(R.layout.preferences);
Button bt=(Button)findViewById(R.id.selectPic);
TextView tv=(TextView) findViewById(R.id.textView1);
String def=getResources().getString(R.string.noDefinido);
tv.setText(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("logo", def));
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName(getApplicationContext(),
SDCardImagesActivity.class.getName());
startActivity(intent);
}
});
//habra que sacarlo de la red
String [] ofis ={"Venus", "Europa"};
ListPreference lp=new ListPreference(this);
lp.setValue("oficina");
lp.setEntries(ofis);
lp.setEntryValues(ofis);
lp.setTitle(R.string.oficina);
lp.setDefaultValue(ofis[1]);
getPreferenceScreen().addPreference(lp);
}
}
It works fine with the xml preferences but I also need to add a preference in code the reason of it is that the options of this ListPreference will come from the database (here I have the String array for now). The problem is the data changed for this preference is not persisted. When I make the changes I press back button I come back the value goes back to default value. How could I make sure this preference is saved too?
Thanks a lot
solved I included the preference without setting up its data then in code:
String [] ofis ={"Venus", "Europa"};
ListPreference lp=(ListPreference) getPreferenceManager().findPreference("oficina");
lp.setEntries(ofis);
lp.setEntryValues(ofis);
lp.setDefaultValue(ofis[1]);
works fine now