Sub Class_Globals
Private Root As B4XView
Private xui As XUI
Private Button1 As Button
#if B4J
Private TextArea1 As TextArea
Private TextField1 As TextField
#Else If B4A
Private TextArea1 As EditText
Private TextField1 As EditText
#End IF
Private RabbitStory As List
Private FoundIndices As List
End Sub
Public Sub Initialize
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
#if B4J
RabbitStory = File.ReadList(File.DirApp, "RabbitStory.txt")
#else If B4A
RabbitStory = File.ReadList(File.DirInternal, "RabbitStory.txt")
#End If
TextField1.RequestFocus
End Sub
Private Sub searchFor(item As String) As List
item = item.ToLowerCase.trim
Dim result As List
result.Initialize
For i = 0 To RabbitStory.Size - 1
Dim line As String = RabbitStory.Get(i)
If line.ToLowerCase.Contains(item) Then result.Add(i)
Next
Return result
End Sub
Private Sub TextField1_TextChanged (Old As String, New As String)
If New.Trim.length > 2 Then 'small terms will select all items in source, so ignore
FoundIndices = searchFor(New)
If FoundIndices.Size > 0 Then
Dim sb As StringBuilder
sb.Initialize
For Each index As Int In FoundIndices
sb.Append(RabbitStory.Get(index)).Append(CRLF).Append(CRLF)
Next
sb.Remove(sb.Length -1, sb.Length)
TextArea1.Text = sb.toString
End If
End If
End Sub
Private Sub Button1_Click
Dim lines As List = Regex.Split(CRLF, TextArea1.Text)
For i = 0 To lines.Size - 1 Step 2
Dim index As Int = FoundIndices.Get(i / 2)
RabbitStory.Set(index, lines.Get(i))
Next
TextField1.Text = ""
TextArea1.Text = ""
TextField1.RequestFocus
#if B4J
File.WriteList(File.DirApp, "RabbitStory.txt", RabbitStory)
#else If B4A
File.WriteList(File.DirInternal, "RabbitStory.txt", RabbitStory)
#End If
End Sub