How to save spinner to saved/shared preferences - android

I am able to save Strings in my saved preferences but having difficulty saving my spinner.
public class Diet extends Activity {
private SharedPreferences sharedPreferences;
Spinner spnCalorieRange;
Here is my onCreate:
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String strAge = Integer.toString(age);
String strHeight = Integer.toString(height);
String strWeight = Integer.toString(weight);
name = loadSavedPreference("name");
strAge = loadSavedPreference("strAge");
strHeight = loadSavedPreference("strHeight");
strWeight = loadSavedPreference("strWeight");
etName.setText(name);
etAge.setText(strAge);
etHeight.setText(strHeight);
etWeight.setText(strWeight);
This is my Spinner in my onCreate:
spinner = (Spinner)findViewById(R.id.spnCalorieRange);
adapter = ArrayAdapter.createFromResource(this, R.array.Calorie_Range, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
long item = parent.getItemIdAtPosition(position);
String pos =spinner.getSelectedItem().toString();
//sharedPreferences.edit().putInt("PREF_SPINNER", position).commit();
if (item == 0){
deficitPercentage = .05;
}
else if (item ==1)
{
deficitPercentage = .1;
}
else if (item ==2)
{
deficitPercentage = .15;
}
else if (item ==3)
{
deficitPercentage = .2;
}
else if (item ==4)
{
deficitPercentage = .25;
}
else
{
deficitPercentage = .3;
}
//editor.putString("pos", pos);
//editor.commit();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
This is in my onCLick behind a button, where I'm saving the strings and spinner
age = (int) Double.parseDouble(etAge.getText().toString());
height = (int) Double.parseDouble(etHeight.getText().toString());
weight = (int) Double.parseDouble(etWeight.getText().toString());
//Save Preferences
String strAge = Integer.toString(age);
String strHeight = Integer.toString(height);
String strWeight = Integer.toString(weight);
name = etName.getText().toString();
savePreference("name",name);
strAge = etAge.getText().toString();
savePreference("strAge",strAge);
strHeight = etHeight.getText().toString();
savePreference("strHeight",strHeight);
strWeight = etWeight.getText().toString();
savePreference("strWeight",strWeight);:

You can't save the actual spinner object to shared prefs, but you can save all of the values that make the spinner work its magic and next time you create the spinner, just apply those values

In your onItemSelected place:
int spinnerPosition = spinner.getSelectedItemPosition();
saveSpinnerPosition(spinnerPosition);
Save method
public void saveSpinnerPosition(int position){
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
prefEditor.putInt("spnCalorieRange",position);
prefEditor.apply();
}
Load method
public void loadSpinnerPosition{
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
int position= sharedPreferences .getInt"spnCalorieRange",-1);
if(spinnerValue > -1)
// set the value of the spinner
spinner.setSelection(position);
}
To put spinners back to position, call loadSpinnerPosition in your onCreate
------------------------------------------------------------------------------------------------------------------------------------
Edit:
because I noticed this post is also your question, u can also do everything at once:
At the top of your activity:
int spinnerPosition;
In your onItemSelected place:
spinnerPosition = spinner.getSelectedItemPosition();
Save method
public void saveMethod(){
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("Gender", radioSexButton.isChecked());
editor.putBoolean("Male", rdoMale.isChecked());
prefEditor.putInt("spnCalorieRange",spinnerPosition);
prefEditor.apply();
}
Load method
public void loadMethod(){
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
int position= sharedPreferences .getInt"spnCalorieRange",-1);
radioSexButton.setChecked(sharedPreferences.getBoolean("Gender", false));
rdoMale.setChecked(sharedPreferences.getBoolean("Male", false));
if(spinnerValue > -1)
// set the value of the spinner
spinner.setSelection(position);
}
Then call saveMethod() when u want to save all your states, and call loadMethod() when u wanna load all your states
Should help u out

Related

how to override getItemPosition in PagerAdapter

I am trying to update position value by overriding getItemPosition() but it is not working. Please help..
Code here -
#Override
public int getItemPosition(Object object) {
super.getItemPosition(object);
SharedPreferences pos = ctx.getSharedPreferences("forposition", MODE_PRIVATE);
int tom = pos.getInt("pos",0);
Log.d("posTom","value : "+tom );
return tom;
}
This is instantiateItem() method :
public Object instantiateItem(#NonNull ViewGroup container, int position) {
inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.swipe,container,false);
ImageView imageView = (ImageView) v.findViewById(R.id.imageview);
SharedPreferences sp = ctx.getSharedPreferences("your_shared_pref_name", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
if((sp.getString("array", null)) == null) {
String temp = Arrays.toString(count);
editor.putString("array", temp);
editor.commit();
}
String fetch = sp.getString("array", null);
Log.d("positionlist ","value : "+fetch);
fetch = fetch.substring(1, fetch.length()-1);
String strarray[] =fetch.split(", ") ;
/* SharedPreferences spt= ctx.getSharedPreferences("your_shared_pref_name", MODE_PRIVATE);
int post = spt.getInt("pos",1);*/
Log.d("position","value : "+position );
Log.d("posvalue","value : "+strarray[position] );
Log.d("poscondition","value : "+(Integer.parseInt(strarray[position]) == 0) );
if (Integer.parseInt(strarray[position]) == 0) {
BackgroundTask task = new BackgroundTask(this);
task.execute();
imageView.setImageResource(img[position]);
container.addView(v);
strarray = setValue(position, strarray);
String temp1 = Arrays.toString(strarray);
editor.putString("array", temp1);
editor.commit();
} else {
imageView.setImageResource(img[position]);
container.addView(v);
}
SharedPreferences pos = ctx.getSharedPreferences("forposition", MODE_PRIVATE);
SharedPreferences.Editor poseditor = pos.edit();
poseditor.putInt("pos", position);
poseditor.commit();
return v;
}
Please give solution how to update position from instantiateItem() method.

How to save Spinner as Shared Preference?

I have been able to save Integers and Strings as Shared Preferences but have searched and cannot seem to be able to save a Spinners selected value as a shared preference?
spinner = (Spinner)findViewById(R.id.spnCalorieRange);
adapter = ArrayAdapter.createFromResource(this, R.array.Calorie_Range, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
long item = parent.getItemIdAtPosition(position);
String pos =spinner.getSelectedItem().toString();
//sharedPreferences.edit().putInt("PREF_SPINNER", position).commit();
if (item == 0){
deficitPercentage = .05;
}
else if (item ==1)
{
deficitPercentage = .1;
}
else if (item ==2)
{
deficitPercentage = .15;
}
else if (item ==3)
{
deficitPercentage = .2;
}
else if (item ==4)
{
deficitPercentage = .25;
}
else
{
deficitPercentage = .3;
}
editor.putString("pos", pos);
editor.commit();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Accessing Shared Preferences here in OnCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_diet);
spnCalorieRange = (Spinner) findViewById(R.id.spnCalorieRange);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences pref = getApplicationContext().getSharedPreferences("Options", MODE_PRIVATE);
editor=pref.edit();
String strAvgCalIntake = Double.toString(dailyCalorieIntake);
String strGoal = Double.toString(goal);
strAvgCalIntake = loadSavedPreference("strAvgCalIntake");
strGoal = loadSavedPreference("strGoal");
etAverageCalorieIntake.setText(strAvgCalIntake);
etLoseWeight.setText(strGoal);
//mPrefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
//spinner.setSelection(mPrefs.getInt(PREF_SPINNER, 0));
// int selectedPosition = sharedpreferences.getInt("spinnerSelection", 0);
int selectedPosition = spnCalorieRange.getSelectedItemPosition();
sharedPreferences.getInt("spinnerSelection", selectedPosition);
((Editor) sharedPreferences).commit();
Here is the Button where I'm saving the shared Preferences:
The Strings are saving fine btw.
Button btnBack = (Button)findViewById(R.id.btnBack);
btnBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String strAvgCalIntake = Double.toString(dailyCalorieIntake);
String strGoal = loadSavedPreference("strGoal");
spnCalorieRange = (Spinner) findViewById(R.id.spnCalorieRange);
strAvgCalIntake = etAverageCalorieIntake.getText().toString();
savePreference("strAvgCalIntake",strAvgCalIntake);
strGoal = etLoseWeight.getText().toString();
savePreference("strGoal",strGoal);
SharedPreferences spref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = spref.edit();
editor.putString("deficitPercentage_key", Double.toString(deficitPercentage)); //
editor.commit();
If you have validated the deficitPercentage value, (and I hope you did)
SharedPreferences spref = getSharedPreferences("your_prefs_name", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = spref.edit();
editor.putString("deficitPercentage_key", Double.toString(deficitPercentage)); //
editor.commit();

Strikethrough an item on a Listview

I need to put a strikethrough on the text after the item has been checked. I found solutions that use setPaintFlags(descriptionView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);. I don't use a textview but instead use simple_list_item_multiple_choice for my listview so how do I solve this? Here is my entire code:
public class Surv_list extends Fragment {
final String[] OPSys = new String[]{"item1","item2","item3","item4"
};
ListView myList;
Button getChoice, clearAll;
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyUserChoice" ;
ArrayList<String> selectedItems = new ArrayList<String>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.listlay, container, false);
myList = (ListView)rootView.findViewById(R.id.list);
ListView list = (ListView) rootView.findViewById(R.id.list);
clearAll = (Button)rootView.findViewById(R.id.clearall);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_multiple_choice, OPSys);
myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
myList.setAdapter(adapter);
list.setAdapter(adapter);
sharedpreferences = getActivity().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
if(sharedpreferences.contains(MyPREFERENCES)){
LoadSelections();
}
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
SaveSelections();
}
});
clearAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ClearSelections();
}
});
return rootView;
}
private void SaveSelections() {
// save the selections in the shared preference in private mode for the user
SharedPreferences.Editor prefEditor = sharedpreferences.edit();
String savedItems = getSavedItems();
prefEditor.putString(MyPREFERENCES.toString(), savedItems);
prefEditor.commit();
}
private String getSavedItems() {
String savedItems = "";
int count = this.myList.getAdapter().getCount();
for (int i = 0; i < count; i++) {
if (this.myList.isItemChecked(i)) {
if (savedItems.length() > 0) {
savedItems += "," + this.myList.getItemAtPosition(i);
} else {
savedItems += this.myList.getItemAtPosition(i);
}
}
}
return savedItems;
}
private void LoadSelections() {
// if the selections were previously saved load them
if (sharedpreferences.contains(MyPREFERENCES.toString())) {
String savedItems = sharedpreferences.getString(MyPREFERENCES.toString(), "");
selectedItems.addAll(Arrays.asList(savedItems.split(",")));
int count = this.myList.getAdapter().getCount();
for (int i = 0; i < count; i++) {
String currentItem = (String) myList.getAdapter()
.getItem(i);
if (selectedItems.contains(currentItem)) {
myList.setItemChecked(i, true);
} else {
myList.setItemChecked(i, false);
}
}
}
}
private void ClearSelections() {
// user has clicked clear button so uncheck all the items
int count = this.myList.getAdapter().getCount();
for (int i = 0; i < count; i++) {
this.myList.setItemChecked(i, false);
}
// also clear the saved selections
SaveSelections();
}
}
any help would be very much appreciated.
You can create a custom adapter for your list view and in the getview method of the adpater take the handle of textview . Based on your condition you can combine the already available code to strikeout the text view.
for making your own view attributes in list view, you have to make a custom Adapter other than using default adapter. there is no way you can do this using default adapter. here you can learn how to make custom adapter. also I would suggest you to use RecyclerView instead of ListView

How to remove SharedPreferences from Array

I have problem with removing items from ArrayList. I tried it maybe 100 times but I can't fix it. Saving to list isn't problem but it's very hard to remove for me.
When I remove SharedPrefs key (position) It's good first time but if I first time remove first position it's deleted from list but its still in preferences so when I try to remove first position second time I cant remove it because there is still saved preference with value "" but I need to remove this preference totally that first position have to contain preferences with value on second position not "".
I tried to make some images for better understanding.
Thats before remove 1st position:
And this is after remove 1st position
There is my CustomListAdapter class
public class CustomListAdapterInterests extends ArrayAdapter < String > {
private final Activity context;
private final ArrayList < String > mItemInterest;
public CustomListAdapterInterests(Activity context, ArrayList < String > itemInterest) {
super(context, R.layout.list_item_interests, itemInterest);
this.context = context;
this.mItemInterest = itemInterest;
}
#Override
public int getCount() {
return mItemInterest.size();
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_item_interests, null, true);
TextView itemInterestTV = (TextView) rowView.findViewById(R.id.textInterest);
itemInterestTV.setText(mItemInterest.get(position));
return rowView;
}
}
And here is my fragment
public class InterestsFragment extends BaseFragment {
private ArrayList < String > mInterestList;
private static final int MAX_STORED_LINES_INTERESTS = 50;
private FloatingActionButton plusInterestsBTN;
private CustomListAdapterInterests adapterInterests;
private ListView listInterests;
private EditText interestET;
private Button confirmInterestBTN;
public SharedPreferences sharedPreferences;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_interests, container, false);
plusInterestsBTN = (FloatingActionButton) v.findViewById(R.id.plusInterests);
sharedPreferences = getActivity().getSharedPreferences(Constants.PREFERENCES_INTERESTS, Context.MODE_PRIVATE);
mInterestList = new ArrayList < String > ();
loadInterestFromPreferences(mInterestList);
adapterInterests = new CustomListAdapterInterests(getActivity(), mInterestList);
listInterests = (ListView) v.findViewById(R.id.listViewInterests);
listInterests.setAdapter(adapterInterests);
listInterests.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView <? > arg0, View v, int position, long arg3) {
if (sharedPreferences.contains(Constants.INTEREST + position)) {
SharedPreferences.Editor editor = sharedPreferences.edit();
mInterestList.remove(position);
adapterInterests.notifyDataSetChanged();
editor.remove(Constants.INTEREST + position);
editor.commit();
}
}
});
listInterests.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {#Override
public boolean onItemLongClick(AdapterView <? > arg0, View arg1,
final int position, long id) {
onShowDialogSetItem(position);
return true;
}
});
plusInterestsBTN.setOnClickListener(new View.OnClickListener() {#Override
public void onClick(View v) {
onShowDialogAddItem();
}
});
listInterests.setOnScrollListener(new AbsListView.OnScrollListener() {#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
int btn_initPosY = plusInterestsBTN.getScrollY();
if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {
plusInterestsBTN.animate().cancel();
plusInterestsBTN.animate().translationXBy(350);
} else {
plusInterestsBTN.animate().cancel();
plusInterestsBTN.animate().translationX(btn_initPosY);
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
return v;
}
private void loadInterestFromPreferences(ArrayList < String > mInterestList) {
for (int x = 0; x < 5; x++) {
String interests = sharedPreferences.getString(Constants.INTEREST + x, Constants.DEFAULT);
Toast.makeText(getActivity(), interests, Toast.LENGTH_SHORT).show();
if (interests != "") {
mInterestList.add(interests);
}
}
}
private void onShowDialogSetItem(final int position) {
final Dialog dialogInterest = new Dialog(getActivity());
dialogInterest.getWindow().getAttributes().windowAnimations = R.anim.abc_slide_in_top;
dialogInterest.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogInterest.getWindow().getAttributes().windowAnimations = R.style.animationName;
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.fragment_interests_add_event, null, false);
dialogInterest.setCanceledOnTouchOutside(true);
dialogInterest.setContentView(view);
final EditText interestET = (EditText) dialogInterest.findViewById(R.id.editTextInterest);
Button confirmInterestBTN = (Button) dialogInterest.findViewById(R.id.confirmInterest);
TextView title = (TextView) dialogInterest.findViewById(R.id.textView2);
title.setText("Edit Interest");
interestET.setText(mInterestList.get(position));
confirmInterestBTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("2", "" + position);
String interest = sharedPreferences.getString(Constants.INTEREST + position, Constants.DEFAULT);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(Constants.INTEREST + position, interestET.getText().toString());
editor.commit();
String interests = sharedPreferences.getString(Constants.INTEREST + position, Constants.DEFAULT);
mInterestList.set(position, interestET.getText().toString());
Toast.makeText(getActivity(), "Upravené: " + interests, Toast.LENGTH_SHORT).show();
adapterInterests.notifyDataSetChanged();
dialogInterest.dismiss();
}
});
dialogInterest.show();
}
private void onShowDialogAddItem() {
if (mInterestList.size() >= MAX_STORED_LINES_INTERESTS) {
return;
}
final Dialog dialogInterest = new Dialog(getActivity());
dialogInterest.getWindow().getAttributes().windowAnimations = R.anim.abc_slide_in_top;
dialogInterest.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogInterest.getWindow().getAttributes().windowAnimations = R.style.animationName;
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.fragment_interests_add_event, null, false);
dialogInterest.setCanceledOnTouchOutside(true);
dialogInterest.setContentView(view);
interestET = (EditText) dialogInterest.findViewById(R.id.editTextInterest);
confirmInterestBTN = (Button) dialogInterest.findViewById(R.id.confirmInterest);
confirmInterestBTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = listInterests.getAdapter().getCount();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(Constants.INTEREST + position, interestET.getText().toString());
editor.commit();
String interests = sharedPreferences.getString(Constants.INTEREST + position, Constants.DEFAULT);
Toast.makeText(getActivity(), "Přidané: " + interests, Toast.LENGTH_SHORT).show();
mInterestList.add(interestET.getText().toString());
//adapterInterests.notifyDataSetChanged();
dialogInterest.dismiss();
}
});
dialogInterest.show();
adapterInterests.notifyDataSetChanged();
}
}
Thank you for help. Sorry for my English. If do you will help me I can do any material design app icon for you or google play designs. Thank you. If there is few informations please say me.
I think if you save all of your string list to single property of preferences will make it easy to manage.
see this sample:
//for save
StringBuilder sb = new StringBuilder();
for (String interest : mInterestList) {
sb.append(interest).append(",");
}
prefsEditor.putString("MyInterests", sb.toString());
prefsEditor.commit();
//for read
String [] interests= sharedPreferences.getString("MyInterests");
mInterestList = new ArrayList<String>(Arrays.asList(interests));
in every change to your mInterestList just save it again. no need to remove and adding. change your mInterestList and save again in shared preferences.
Looks to me like your adapter runs off of mInterestList .
I don't see you removing the data item from mInterestsList when your remove the Preference?
Rather than checking whether shared preferences contains, see if it is set to null instead or not, that is do,
if (sharedPreferences.getString(Constants.INTEREST + position)!=null) {
SharedPreferences.Editor editor = sharedPreferences.edit();
mInterestList.remove(position);
adapterInterests.notifyDataSetChanged();
editor.remove(Constants.INTEREST + position);
editor.commit();
}

How to save checkbox items to SharedPreferences?

I need to save values from the checkbox to shared preferences so that even after exiting, the checked boxes are still checked. Could anyone please show me how to solve this issue?
public class MainActivity extends Activity implements AdapterView.OnItemClickListener {
ListView listView;
ArrayAdapter<Model> adapter;
List<Model> list = new ArrayList<Model>();
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.my_list);
adapter = new MyAdapter(this,getModel());
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
TextView label = (TextView) v.getTag(R.id.label);
CheckBox checkbox = (CheckBox) v.getTag(R.id.check);
Toast.makeText(v.getContext(), label.getText().toString() + " " + isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();
}
private String isCheckedOrNot(CheckBox checkbox) {
if(checkbox.isChecked())
return "is checked";
else
return "is not checked";
}
private List<Model> getModel() {
list.add(new Model("1"));
list.add(new Model("2"));
list.add(new Model("3"));
return list;
}
}
I made this class, use it:
public class SavePreferences {
private final static String MYAPP_PREFERENCES = "MyAppPreferences";
public void savePreferencesData(View view, String KEY, String TEXT) {
SharedPreferences prefs = view.getContext().getSharedPreferences(MYAPP_PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
if (TEXT != null && KEY != null) {
editor.putString(KEY, TEXT);
editor.commit();
}
}
private String loadPreferencesData(View view, String KEY){
SharedPreferences prefs = view.getContext().getSharedPreferences(MYAPP_PREFERENCES, Context.MODE_PRIVATE);
String data = prefs.getString(KEY, "No Data!");
return data;
}
}
And then:
savePreferencesData(View, "CheckBox1", "true");

Categories

Resources