Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically changing SharedPreferences
    primarykey
    data
    text
    <p>I am currently working on an app with SharedPreferences and I am trying to dynamically change the value that appears on the Prefs activity screen. On the screen I have a preferencelistview which contains months of the year and two edit boxes to store info in relation to chosen month. Upon selection of a month, I want a few things to happen:</p> <ol> <li>Inserts a row in a database with the selected month (if it had not been previously selected) -- This I can get to work just peachy</li> <li>Changes the values of the sharedpreference edit boxes to the values stored in the row of the database --this is where I am having trouble</li> <li>When the editboxes are updated, they should update the corresponding row in the db based on the selected month -- working without issue.</li> </ol> <p>So, basically, my problem is with item#2. I have a function that checks when a new month is selected from the list:</p> <pre><code>public void setMonth(SharedPreferences sp) { ContentValues cv = new ContentValues(); cv.clear(); int sel_month = Integer.valueOf(sp.getString("pActivityMonth", String.valueOf(this.month))); //listvew works with strings, convert to int int stored_new = 0; //default value int stored_ren = 0; //default value Log.d(TAG, String.valueOf(sel_month)); String[] col = { Database.C_MONTH, Database.C_QNEW, Database.C_QRENEW }; String[] arg = { String.valueOf(sel_month) }; Cursor c = db.query(Database.RR_TABLE, col, Database.C_MONTH + "=?", arg, null, null, null); if (c.getCount()&lt;1) { //New month, insert row cv.put(Database.C_MONTH, sel_month); db.insertWithOnConflict(Database.RR_TABLE, null, cv, SQLiteDatabase.CONFLICT_IGNORE); } else { //row exists, get stored values Log.d(TAG, "cursor count: " + String.valueOf(c.getCount())); c.moveToFirst(); stored_new = c.getInt(c.getColumnIndex(Database.C_QNEW)); stored_ren = c.getInt(c.getColumnIndex(Database.C_QRENEW)); Log.d(TAG, "stored_new: " + String.valueOf(stored_new)); } //Change edittext preferences to stored values editor.putString("pNewQuota", String.valueOf(stored_new)); editor.putString("pRenewQuota", String.valueOf(stored_ren)); editor.apply(); editor.commit(); } </code></pre> <p>So, I select a new month (july), click on one of the edittext options to update the info (lets say I entered 20), then select a different month (August), with a default value of 0. If I then click on the edittext again, the dialog continues to show 20 as opposed to 0. If I change this 0 to something else, it stores in the correct row of the database, but then this value continues to persist after changing to another month (say, back to July). My Logcat shows that the function is grabbing the correct values, and that the putString is being run. Is the PreferenceScreen not able to refresh itself? Thanks for any help. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload