I have preference defined in xml, and I do this addPreferencesFromResource(R.xml.preferences1); to create the pref activity. But in code (dynamically) at runtime somethimes(depends on busyness logic) I add more items like this
CheckBoxPreference c=new CheckBoxPreference(this);
c.setKey("asdasd");
c.setTitle("asd");
getPreferenceScreen().addPreference(c);
This works great and everything is fine(the state is saved corectlly and it stays persistent) until I restart the phone. When I restart the phone this newly added item is lost. And only items from the xml file are shown in the prefereceActivity.
My question is what should I do to have this items even when the user restart the phone.
Related
I am making a todo app. I made a shared preference to save the data to internal storage an an arryadapter to manage my data. Everything works well. I added a strikethrough functionality and it works, but when I exit and reopen the app the strikethrough is gone and I don't know how to restrike through the todos in my listview.
I can populate my listview with the data I extracted from my shared preferences. It shows but it does not strikethrough.
When I tried to access listview getchild, I get a null exception
Whenever you are checking the task, save its check status in the shared preference depends on check status you can perform strikethrough
I wrote a very simple FMX Adroid App, the function is:
Show Form 2 then write something to record(include title and detail text),
close Form 2 to Main Form, then make a checkbox in Main Form with the title we just recorded in Form 2.
if user check the checkbox, then press "del" buttn then delete the record file and checkbox.
the problem is:
when closed Form 2 and in MainForm::OnActivate we can add a new checkbox for the record.
if we checked checkbox then clicked delete, free the pointer of checked checkbox, the checkbox still in main form until I reopen the APP.
I tried:
Invalidate();
Application->ProcessMessages();
BeginUpdate();
EndUpdate();
Still can't work
does anyone know what's going on ? why FMX TForm member has no "Repaint()" or "Update()" "Refresh()" ? just like VCL has.
If you want your TCheckBox* (or any other control) disappear from a Form, you need to set its Parent property to nullptr before deleting it. If you created your control in runtime using new please remember to call delete.
//init
TCheckBox* checkBox = new TCheckBox(Form2);
//delete
checkBox->Parent = nullptr;
delete checkBox;
Answering the second part of your question, you can call Invalidate() function to repaint your whole Form (but first see first part of this answer). But I think it will run properly without calling this function.
Your controls have Repaint() member and it may be better to call them instead, ie. if your checkbox was placed in TPanel*, repainting only this panel is better idea than repainting whole form.
My app has this appearance
It seems to be a TableLayout with several TableRows. In my activity, each TableRow has 3 views: ImageView, TextView and a Button.
The user is the one who sets the content of the ImageView and the TestView by entering the text he wants and pressing a button.
I store this data in stringArray variables and works fine if the phone is not restarted or the app is not closed (forceClose)
If one of these two situations happen, i lose all my data.
I've been trying to store my StringArrays by SharedPreferences but I don't know when i should load the preferences, whether it's in OnCreate() or OnResume() or OnStart methods().
Another question is how to define the arrays. I use this:
String[] titulo = new String[500];
I don't know if this string is created each time i start the activity. Because what i want is to load the previous String (from SharedPreferences) and add some more entries not to create new ones every time the phone is rebooted, for example.
Do you think i need a SQL database or it's OK with this StringArrays.
Thank you.
You should probably switch to using a database, seeing as you have an array of size 500, which could possibly increase in the future.
However, if you want to continue using SharedPreferences, you should write the data in onPause() and onStop() methods, and use an if else statement to see if your data is null before running an operation on it. If the data is null, the read it from the SharedPreferences before continuing.
I have added a new setting to the Android 4.0.4 Contacts source, it's called useCompactView. What it does is reduce the size of the contact photos so you can fit twice as many contacts on the screen in list view.
<CheckBoxPreference
android:key="useCompactView"
android:defaultValue = "false"
android:title="#string/display_options_use_compact_view"
android:summary="#string/display_options_use_compact_view_summary" />
In ContactsPreferenceActivity I added a listener for my new checkbox and this does get fired off, I tested with a Log.d message. I tried to get the listview and call invalidateViews() on it but that doesn't work either. I am sure there has to be a way to get the adapter and force a refresh on it.
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
sharedPreferences.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if("useCompactView".equals(key)) {
getListView().invalidateViews(); // This does not work
}
}
});
What I am trying to accomplish is; when the user selects Use compact view in settings, the display will be refreshed automatically. Basically I need to redraw the screen. If I go into settings, check Use compact, then also change the sort order, I get the compact view when I hit the back button. So changing the sort order does refresh the display/listview. But I don't know how to do this from my preference activity.
If you need more code, please tell me and I will put whatever you need. This is a real head scratcher.
I have Listpreferences in my app. They don't appear to be setting to their defaults right after installation - they appear to be null. I'm trying to figure out why my default preferences are not being set right after installation. In my main code I have:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
InUnits = sp.getString("List1", "defValue");
InAngs = sp.getString("List2", "defValue");
OutUnits = sp.getString("List3", "defValue");
OutAngs = sp.getString("List4", "defValue");
Right after the above code executes, each variable contains "defValue" instead of the actual values I have assigned in my ListPreference below.
My preference xml file is called, "settings.xml". Here's what one of the ListPreferences there looks like:
<ListPreference
android:key="List1"
android:title="Input: Alph"
android:summary="Choose Alph or Ralph"
android:entries="#array/inputAlph"
android:entryValues="#array/input_Alph_codes"
android:dialogTitle="Input Alph"
android:defaultValue="ININ"/>
Here's what some of my strings.xml file looks like:
<string-array name="inputUnits">
<item>Alph</item>
<item>Ralph</item>
</string-array>
<string-array name="input_Alph_codes">
<item>ININ</item>
<item>INMM</item>
</string-array>
When I go to menu, and then settings, I can see my defaults checked (radiobuttoned). Then when I go back from the settings menu to my main screen - all is well - for life! ...then each var above is assigned the proper default value.
This only happens when I first install my app on the phone. After I go to the settings screen once and then right out of it, the app is fine and accepts any setting changes.
By the way, as you can see, "List1" is the android:key within a file called settings.xml in my res/xml folder.
They don't appear to be setting to
their defaults right after
installation - they appear to be null.
That's what's supposed to happen.
I'm trying to figure out why my
default preferences are not being set
right after installation.
They're not supposed to be. The preference XML you have listed there is only used for populating a PreferenceActivity, nothing more. Until the user opens the PreferenceActivity, the preferences will be null, and the defaults you supply to the SharedPreferences getters will be returned.
UPDATE
You can use setDefaultValues() on PreferenceManager to assign the defaults from your preference XML to a SharedPreferences. However, be careful of the timing -- this will do disk I/O, and therefore ideally is performed on a background thread.
Set the default values to SharedPreferences from your preference XML.
PreferenceManager.setDefaultValues(Context context, int resourceId, boolean readAgain)
PreferenceManager.setDefaultValues
You can specify a default value like this
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
pref.getString("thePrefKey", "theDefaultValue");
The android:defaultValue="..." in the "layout" settings.xml is only a visual help for user