Android Question [B4A] PrefDialog.PrefItems.Set

T201016

Active Member
Licensed User
Longtime User
Hello,
Can someone explain to me what is wrong with the performance,
Look:
PrefDialog.PrefItems.Set(0, newItem) ?

B4X:
    ...


    PrefDialog.Initialize(Parent, "Dialog", 300dip, 400dip)

    PrefDialog.LoadFromJson(File.ReadString(File.DirAssets, "template.json"))

 

    ...

 

    Dim oldItem As String = PrefDialog.PrefItems.Get(0)

    Dim newItem As Object = SFX.MidExtract(oldItem,"Title=","]","Duplicate Record")

    PrefDialog.PrefItems.Set(0, newItem) ------------------ >>> ERROR

java.lang.ClassCastException:
java.lang.String cannot be cast to b4a.example.preferencesdialog

$_b4xprefitem



Used procedures:

B4X:
'Used to extract text from a string between to other blocks of text,
'and converted to a new text last parameter function.
Public Sub MidExtract(paramString1 As String, paramString2 As String, paramString3 As String, paramString4 As String) As Object
    Private str As String = ""

    If (LenString(paramString1) = 0) Then Return ""
    If paramString2.Length = 0 And paramString3.Length = 0 Then    Return paramString1
    If ((paramString1.Contains(paramString2)) And (paramString1.Contains(paramString3))) Then

        str = paramString1.Substring2(paramString1.IndexOf(paramString2), paramString1.indexOf(paramString3))
        str = str.Substring2(paramString2.Length, str.Length).Trim
      
        str = paramString1.Replace(str, paramString4).Trim
    
        Return str
    Else
        Return ""
    End If
End Sub

'Returns the length of this string.
Public Sub LenString(paramString1 As String) As Int
    Return paramString1.Length
End Sub

template.json

B4X:
{
    "Version": 1.1,
    "Theme": "Light Theme",
    "Items": [
        {
            "title": "Edit Record",
            "type": "Separator",
            "key": "",
            "required": false
        },
        {
            "title": "First Name",
            "type": "Text",
            "key": "FirstName",
            "required": true
        },
        {
            "title": "Last Name",
            "type": "Text",
            "key": "LastName",
            "required": true
        },
        {
            "title": "Company",
            "type": "Text",
            "key": "Company",
            "required": true
        },
        {
            "title": "Address",
            "type": "Text",
            "key": "Address",
            "required": true
        },
        {
            "title": "Email",
            "type": "Text",
            "key": "Email",
            "required": true
        }
    ]
}
 
Last edited:

T201016

Active Member
Licensed User
Longtime User
Hello Erel,

3.
In the file 'template.json'
I want to replace the title of a dialog
from "title": "Edit Record" to "title": "xxxxxxxxxx xxxxx"
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
As I wrote, the first step is to remove this MidExtract code. It is really impossible to fix any code based on it.

A simple solution is to parse the json file, replace the value in the map and generate a new json string.

B4X:
Dim parser As JSONParser
parser.Initialize(s)
Dim m As Map = parser.NextObject
Dim items As List = m.Get("Items")
For Each item As Map In items
    If item.Get("title") = "Edit Record" Then
        item.Put("title", "new title")
    End If
Next
Dim gen As JSONGenerator
gen.Initialize(m)
Log(gen.ToPrettyString(4))
 
Upvote 0

T201016

Active Member
Licensed User
Longtime User
Welcome back,
Thanks to Erel for solving the problem, and I will check the example below soon.

Your code seems to be working :) so the thread can be closed.
 
Upvote 0
Top