AHPreferenceManager Problem

mrossen

Active Member
Licensed User
Longtime User
Hi,

When I have made changes to the values in the keys and return to the main activity I get the value 0 when I use:

B4X:
manager.GetUpdatedKeys.Size

I can get manager.GetAll.Size to work but not manager.GetUpdatedKeys.Size?

Mogens
 
Last edited:

mrossen

Active Member
Licensed User
Longtime User
Hi again,

I have used the sample application and added this to line 101 in Handlesettings sub

B4X:
Log("Updated Settings : " & manager.GetUpdatedKeys.Size)

When I changing the values on the settings screen I am expecting manager.GetUpdatedKeys.Size to show the number of value I have changed?

Or have I got this wrong.

I will use this to catch the events in the setting screen

I am planing to use this to see what have changed in the settings menu when it is leaved. I have understand that there i no events in the menu.

Mogens
 

Attachments

  • pa.zip
    6.7 KB · Views: 183
Upvote 0

corwin42

Expert
Licensed User
Longtime User
Strange.

I can confirm that your program does not work.

I did make a similar change and it works. I can't see any real difference between your example and my example but mine works and yours not.

@Erel:
Can you have a look at this? I can't find the problem
 

Attachments

  • pa2.zip
    6.7 KB · Views: 178
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This issue is described here: android - SharedPreferences.onSharedPreferenceChangeListener not being called consistently - Stack Overflow

I've updated PreferenceActivity to fix it. Markus, you should change the constructor to:
B4X:
private final OnSharedPreferenceChangeListener listener;
public PreferenceManager() {
         listener = new OnSharedPreferenceChangeListener() {

            @Override
            public void onSharedPreferenceChanged(
                  SharedPreferences sharedPreferences, String key) {
               updatedKeys.add(key);
            }
            
         };
         sp.registerOnSharedPreferenceChangeListener(listener);
      }
 
Upvote 0

mrossen

Active Member
Licensed User
Longtime User
Hi Markus and Erel,

Thanks for your help.

Now the values returning correct.

Mogens
 
Upvote 0

mrossen

Active Member
Licensed User
Longtime User
Hi Again.

I now get the right values for the manager.GetUpdatedKeys.Size.

But I now Have another problem.

When I try to list the keys there is updated the array is empty.

I try to doing something like this

B4X:
Log("Size: " &  manager.GetUpdatedKeys.Size)
   Log("Array: " &  manager.GetUpdatedKeys)
   
   If manager.GetUpdatedKeys.Size > 0 Then
      For i = 0 To manager.GetUpdatedKeys.Size
       Log(manager.GetUpdatedKeys.Get(i))
      Next 
   End If
 
Upvote 0

mrossen

Active Member
Licensed User
Longtime User
Hi,

Now I see the light

What to do is:

B4X:
 Dim l As List

 l.Initialize
 l = manager.GetUpdatedKeys
 
Last edited:
Upvote 0

Laurent95

Active Member
Licensed User
Longtime User
Thanks.

Library updated.

Hello,

That's really weird i have the same problem, of course i have the updated library :BangHead:

I have try to use a locale copy of manager it's the same, i have try to fill another list with the manager.GetUpdatedKeys, the list have also a size at 0.

I can't have a size other than 0, even i have one or several item's values updated in the list of manager.GetUpdatedKeys.

Can you help me ?
Below my code and the filtered log, we can see the manager.GetUpdatedKeys who have item's values updated, but the size returned is always 0 ?????

Any idea, thanks.

Edit : I have change the code and do some test for see. It's the same the manager.GetUpdatedKeys list is initialised and return some keys but the size stay 0 all time then i have error when i try to work with it. The new code is under the first log and the filtered code at bottom. I really don't understand why.

Code :
Sub Activity_Resume
'Do comments
Log("manager UpdatedKeys = " & WidgetService.manager.GetUpdatedKeys)
Dim l As List
l.Initialize
l.AddAll(WidgetService.manager.GetUpdatedKeys)
'Do comment
Log("Size of list = " & l.Size)
Dim x As Int
For x = 0 To l.Size
Log("Value of GetUpdateKeys(" & x & ") = " & l.Get(x))
Select Case l.Get(x)
Case "checkInternet"
WidgetService.InternetChecked = WidgetService.manager.GetBoolean("checkInternet")
Case "checkSeparate"
WidgetService.ThousandSep = WidgetService.manager.GetBoolean("checkSeparate")
Case "checkVibrate"
WidgetService.VibrateCheck = WidgetService.manager.GetBoolean("checkVibrate")
Case "ListMoneyRef"
CallSubDelayed(WidgetService, "imgRefresh_Click")
Case "listCurrencies"
CallSubDelayed(WidgetService, "imgRefresh_Click")
End Select
Next
End Sub

Log :

** Activity (prefactivity) Pause, UserClosed = false **
** Activity (prefactivity) Resume **
manager UpdatedKeys = (ArrayList) [listMoneyRef, checkVibrate, checkInternet]
Size of list = 0
prefactivity_activity_resume (B4A line: 29)
Log("Value of GetUpdateKeys(" & x & ") = " & l.Get(x))

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
at java.util.ArrayList.get(ArrayList.java:311)
at anywheresoftware.b4a.objects.collections.List.Get(List.java:107)
at lizaprod.b4a.widget.moneyconverter.prefactivity._activity_resume(prefactivity.java:285)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:154)
at lizaprod.b4a.widget.moneyconverter.prefactivity$ResumeMessage.run(prefactivity.java:192)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

Code 2 :
Sub Activity_Resume
'Do comments
Log("manager GetUpdatedKeys = " & WidgetService.manager.GetUpdatedKeys)
Log("Size of list of preference= " & WidgetService.manager.GetUpdatedKeys.Size)
Log("IndexOf preference checkInternet = " & WidgetService.manager.GetUpdatedKeys.IndexOf("checkInternet"))
Log("IndexOf preference checkSeparate = " & WidgetService.manager.GetUpdatedKeys.IndexOf("checkSeparate"))
Log("IndexOf preference checkVibrate = " & WidgetService.manager.GetUpdatedKeys.IndexOf("checkVibrate"))
Log("IndexOf preference listMoneyRef = " & WidgetService.manager.GetUpdatedKeys.IndexOf("listMoneyRef"))
Log("IndexOf preference listCurrencies = " & WidgetService.manager.GetUpdatedKeys.IndexOf("listCurrencies"))
Log("GetUpdatedKeys.IsInitialized = " & WidgetService.manager.GetUpdatedKeys.IsInitialized)

Dim x As Int
For x = 0 To WidgetService.manager.GetAll.Size -1
'Log("Value of GetString(" & x & ") = " & WidgetService.manager.GetString(x))
Select Case WidgetService.manager.GetUpdatedKeys.Get(x)
Case "checkInternet"
WidgetService.InternetChecked = WidgetService.manager.GetBoolean("checkInternet")
Case "checkSeparate"
WidgetService.ThousandSep = WidgetService.manager.GetBoolean("checkSeparate")
Case "checkVibrate"
WidgetService.VibrateCheck = WidgetService.manager.GetBoolean("checkVibrate")
Case "listMoneyRef"
CallSubDelayed(WidgetService, "imgRefresh_Click")
Case "listCurrencies"
CallSubDelayed(WidgetService, "imgRefresh_Click")
End Select
Next
End Sub


Log 2 :
** Activity (prefactivity) Pause, UserClosed = false **
** Activity (prefactivity) Resume **
manager GetUpdatedKeys = (ArrayList) [listMoneyRef, checkSeparate, checkVibrate]
Size of list of preference= 0
IndexOf preference checkInternet = -1
IndexOf preference checkSeparate = -1
IndexOf preference checkVibrate = -1
IndexOf preference listMoneyRef = -1
IndexOf preference listCurrencies = -1
GetUpdatedKeys.IsInitialized = true
prefactivity_activity_resume (B4A line: 31)
Select Case WidgetService.manager.GetUpdatedKeys.Get(x)

java.lang.IndexOutOfBoundsException: Invalid location 0, size is 0
at java.util.ArrayList.get(ArrayList.java:341)
at anywheresoftware.b4a.objects.collections.List.Get(List.java:107)
at lizaprod.b4a.widget.moneyconverter.prefactivity._activity_resume(prefactivity.java:292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:154)
at lizaprod.b4a.widget.moneyconverter.prefactivity$ResumeMessage.run(prefactivity.java:192)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
java.lang.IndexOutOfBoundsException: Invalid location 0, size is 0
 
Last edited:
Upvote 0
Top