I have created activity which extends preferenceActivity. In that activity I have opened custom dialog which shows list of item. Whenever i click on item in listview , dialog closed. At that time i want to store the selected item in SharedPreference so that i an get that item in application wide.
But my problem is How do i store that selected item in SharedPreference ??
And when i open my application next time, i should able to retrive previous seleced item.
Here is my code for PreferenceActivity :
public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener
{
SharedPreferences myprefs;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.settings);
addPreferencesFromResource(R.xml.prefs);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
Preference prefereces=findPreference("alertdialog_Font_Style");
prefereces.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
// TODO Auto-generated method stub
DialogFontStyle objdialog = new DialogFontStyle(SettingsActivity.this,R.style.CustomDialogTheme);
objdialog.show();
return true;
}
});
}
Here is my class for dialog :
public class DialogFontStyle extends Dialog {
public DialogFontStyle(Context context, int theme) {
super(context, theme);
// TODO Auto-generated constructor stub
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.dialog_layout);
final String[] sizeType = new String[] {"a" , "b" , "c" , "d"};
final ListView lst = (ListView)findViewById(android.R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String> (context,android.R.layout.simple_list_item_1,sizeType);
lst.setAdapter(adapter);
lst.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
dismiss();
}
});
}
}
Please give me some solution to store data in sharedpreference.
Thanx.
To write something to share preferences use this code:
mContext should be context of your activity or application not dialog. Or if your dialog is within your activity use
SettingsActivity.thisor create class member mContext and initi in ononCreate()