Android Question How to manually get items from parse?

anOparator

Active Member
Licensed User
Longtime User
Am sending the XML content item to EditText1 with a button_click event. The last content item gets to EditText1, the log shows the year item incrementing but not the content item.

B4X:
<?xml version="1.0"?>
<YEARS>
<year>0</year>
<content>First item from the list.</content>
<year>1</year>
<content>Second item from the list.</content>
<year>2</year>
<content>Third item from the list.</content>
<year>3</year>
<content>Forth item from the list.</content>
</YEARS>

B4X:
Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
   
If year = n AND Name = "content" Then
  content = Text
  End If
     EditText1.Text=content
End Sub

   Sub nuVrs_Click
   Log ("CLICK and content" & content)
     Log ("CLICK and year" & n)
   Log ("after + content" & content)
     EditText1.Text=content       'CONTENT  ONLY
   n = n + 1                ' Already tried    n = n - 1
End Sub

help me make sense of incrementing the content item please.
ps: I have read lots of XML articles in this loverly forum.
Thanks in advance
 

Attachments

  • XMLtest.zip
    10.4 KB · Views: 110

sorex

Expert
Licensed User
Longtime User
not ideal but this should work (don't forget to change the event name in the designer)

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("myX")
    If FirstTime Then
     parser.Initialize()
     parser.parse(File.OpenInput(File.DirAssets, "vers.xml"), "parser")
     End If
End Sub
                 
Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
If Name="year" Then year=Text
If year = n AND Name = "content" Then content = Text
EditText1.Text=content
End Sub

Sub nxt_Click
n = n + 1
parse
End Sub

Sub prev_Click
n = n - 1
parse
End Sub

Sub parse
parser.Parse(File.OpenInput(File.DirAssets, "vers.xml"), "parser")
End Sub
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
Thanks a lot sorex , it works like a charm. I'm going read it a few more times though.
Can you tell me how year is a String in the second line, and an Int in the third line.
B4X:
Sub Parser_EndElement (Uri AsString, Name AsString, Text AsStringBuilder)
If Name="year"Then year=Text
If year = n AND Name = "content"Then content = Text
I'm still tweaking the counters as shown below.
B4X:
Sub veRchg_Click
     Dim veRchg As Button
   veRchg = Sender
If  n <= 2 AND veRchg.Tag ="increase" Then    'should be <= 3   
   n = n + 1
   Log("AFTER +" & n)
   parse
End If
If n >= 1 AND veRchg.Tag ="decrease" Then
  n = n - 1
  Log("AFTER -" & n)
      parse
End If
 
Upvote 0

James Chamblin

Active Member
Licensed User
Longtime User
Can you tell me how year is a String in the second line, and an Int in the third line.
year is an int in both lines. When you assign a string to an int, it is cast behind the scenes to an int.

Text = "1" 'A String
year = Text 'The String "1" is converted to the int 1 and assigned to year
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
each view has its seperate event set so you could leave it splitted (as in my example) instead of merged like you do in post #3.

Then you don't need the tags or extra checks on it.
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
year is an int in both lines. When you assign a string to an int, it is cast behind the scenes to an int.

Text = "1" 'A String
year = Text 'The String "1" is converted to the int 1 and assigned to year
wow , and thanks. My next project will be re-reading about assignments :)
each view has its seperate event set so you could leave it splitted (as in my example) instead of merged like you do in post #3.

Then you don't need the tags or extra checks on it.
Thanks again, I wondered if tags added weight and extra processing cycles to the app.

soLVed
 
Upvote 0
Top