android custom list adapter to remember databse id - android

How do I create a list adapter that remember the id as well as the content?
I want to make it in a way that when I initialize the adapter, I will feed it a map with id as the key and the content as the value, as opposed to just a list of strings.
What do I have to subclass, and what I put for the constructor?

You can try that with sharePreferneces. It tried it works fine for me.
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putInt(key,value);
prefsEditor.commit();

Related

How to delete a single value from a shared preference that has multiple values within a single key

I am using shared preferences to store different languages a user enters and display them in a recyclerView . when the users clicks the remove button , i want to remove the selected value from the shared pref
I have written the following code but i don't know where i went wrong
pref = contexts.getSharedPreferences("user", 0);
gson = new Gson();
json = pref.getString("language" , "");
Type type = new TypeToken<List<String>>() {}.getType();
List<String> DataPackage = gson.fromJson(json, type);
String item = DataPackage.get(getAdapterPosition());
String items = pref.getString("language" , item);
editor = pref.edit();
editor.remove(items);
editor.commit();
I want the single item from the pref to be removed but nothing happens , it dosn't remove the data
After removing the item from list , Save the current in sharedPref with same key . This will replace the previous list with current .
And try to use "apply" instead of "commit" when applying any changes .This will update async.
For further you may check the following post :
Remove one object from Arraylist<object> saved in sharedPreferences

How to retrieve spinner value from sharedpreferences? ANDROID

Hello I have a profile layout file wherein it saves the data the user inputs. This layout has checkboxes, textviews, and spinners. Then it will be viewed in my userprofile layout file as in textviews. I'm saving them to sharedpreferences. What I'd like to do is retrieve the String value the user selected/saved from spinners, display it in a textview. How can I do this?
I'm wondering how to implement this:
tvbloodtype.setText(prefs.getInt("bloodtype",0));
Any help is truly appreciated. Thanks.
I got error:
ResourcesNotFoundException: String resource ID
After you have your spinner object you can call yourSpinner.OnItemSelectedListener() to get the object that was selected. Then you can save your item in your TextView
yourSpinner.OnItemSelectedListener(new OnItemSelectedListener {
public void onItemSelected(AdapterView <? > parentview, View v, int position, long id) {
curPos = position;
String selected = parentView.getItemAtPosition(position).toString();
tvbloodtype.setText(selected);
}
});
Also reference: How do you get the selected value of a Spinner?
you will need to use String.valueOf() or Integer.toString() for showing Integer in TextView . Example :
tvbloodtype.setText(String.valueOf(prefs.getInt("bloodtype",0)));
OR
tvbloodtype.setText(Integer.toString(prefs.getInt("bloodtype",0)));
write preferences like
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(key, value); //key="YOUR_KEY_STRING" //value = your boolean value
editor.apply();
read from preferences
Boolean yourBoolean = prefs.getBoolean("YOUR_KEY_STRING",false); //false = value, if value wasn t set
whenever you are saving your value to sharedPreferences.
SharedPreferences prefs= getSharedPreferences("prf", MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putString("bloodtype", spinner.getSelectedItem().toString());
edit.commit();
when you are retriving
tvbloodtype.setText(spinner.getString("bloodtype", ""));

save value of spinner selected item

How can I save the current selected spinner value, such that when I reopen the application the saved value is automatically selected by default?
Please Write below code on onItemSelectedListener() of spinner and store selected value into shared preferences.
String mSpnValue=mSpinner1.getSelectedItem().toString();
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("Value", mSpnValue);
Use below code for set item as selected in spinner.
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String myString = myPrefs.getString("Value","nothing"); // the value you want the
ArrayAdapter<String> myAdap = (ArrayAdapter<String>) mSpinner1.getAdapter();
int spinnerPosition = myAdap.getPosition(myString);
// set the default according to value
mSpinner1.setSelection(spinnerPosition);
You can save spinner position in preferences and when entering back use spinner.setSelection(position_from_preferences);
You can use several methods
For example you can use a DataBase and save on it.
Other methods, and the best IMO, is used SharedPreferences
http://developer.android.com/intl/es/reference/android/content/SharedPreferences.html
http://developer.android.com/intl/es/reference/android/app/backup/SharedPreferencesBackupHelper.html
For set value one another way:---
for(int i=0;i<adapter.getCount();i++){
if(adapter.getItem(i).equals(your save preference value){
spinner_timer.setSelection(i);
}
}
you can also refer to the spinner value by position. that way you need only to deal with ints straight-fowardly:
SharedPreferences settings = getSharedPreferences("MYPREFS", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("VALUE", spinner.getSelectedItemPosition());
editor.commit();
and to load:
spinner.setSelection(settings.getInt("VALUE", 0));

SharedPreferences TextView

I have button, after I click button it will list to the ListView, choose ListView item and this item will display to left of the button, I want after I restart the program the listItem was choosed is display.
If anybody know this, please help me? Thanks
There are 2 possible simple ways:
SharedPreference (simplest)
SQLite (maybe for your purpose is useless)
If you have to save only text chosen from a spinner, well, SharedPreference could be the solution. This is how to use it:
READING VALUE from your PREFERENCES_LIST
// set name of your preferences list
private static String MY_PREFERENCES = "my_preferences_list";
// set key for retrieving text you saved
private static String TEXT_DATA_KEY = "last_text_spinner_choise";
// create a pointer to your preferences list specifying name and access typology
SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
// get String from preferences list specifying KEY of Text you want retrieve and a default string if key doesn't exist
String textData = prefs.getString(TEXT_DATA_KEY, "No Preferences!");
// your textView or something else
TextView outputView = (TextView) findViewById(R.id.outputData);
// set textView with value taken from your preference list
outputView.setText(textData);
WRITING VALUE in your PREFERENCES_LIST
// Same as before
SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
// Editor pointer for changing value in your preference list
SharedPreferences.Editor editor = prefs.edit();
// your editText or spinner from which you want take text value
EditText outputView = (EditText) findViewById(R.id.inputData);
CharSequence textData = outputView.getText();
if (textData != null) {
// Here you save textData in your preferences_list. You have to specify key and value. Key is important for retrieving text as seen before
editor.putString(TEXT_DATA_KEY, textData.toString());
// Confirm your choice by commit()
editor.commit();
}
Hope it helps

Android : Value of check box in SharedPreferences is not reflected at UI

In onResume of my PrefereneActivity, I have the following code :
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean isEnabled = sp.getBoolean("check_enabled", false);
The value of isEnabled is false, however, in the UI the checkbox is still shown as selected. Why is it so?
Do you call mCheckBox.setChecked(isEnabled); in onResume() of your activity?
mCheckBox is your checkbox.
I think you are not using Editor.commit(); after putting the values..
Editor editor = mypreferences.edit();
editor.putBoolean("check_enabled", checkboxb.isChecked());
editor.commit();

Categories

Resources