Android Question How to link mulity string in to proceed same activity

jai76

Member
Licensed User
Longtime User
Dear all,
Good day, I'm a very fresh with the b4a.
Currently i tried to create a dictionary.
I'm making a words index for easier search, now having about 2000 words in one activity.
My code is over then 4000 row. causing b4a an error during compiling.
My question is :- from the attached code, how can i combine the code become as shorter,
for example i wanted to be like code under below. shorter code.
if malay.display.text = ("malay1", "malay2", "malay3", "malay4")
Startactivity("A")
Activity.finish
Else if malay.display text= ("malay8", "malay9", "malay10", "malay11")
Startactivity("b")
Activity.finish
end if

Please advice, thanks in advance

B4X:
Sub Imv1_Click
If MalayDisplay.Text = "malay1" Then
StartActivity("A")
Activity.finish
Else If MalayDisplay.Text = "malay2" Then
StartActivity("A")
Activity.finish
Else If MalayDisplay.Text = "malay3" Then
StartActivity("A")
Activity.finish
Else If MalayDisplay.Text = "malay4" Then
StartActivity("A")
Activity.finish
Else If MalayDisplay.Text = "malay8" Then
StartActivity("b")
Activity.finish
Else If MalayDisplay.Text = "malay9" Then
StartActivity("b")
Activity.finish
Else If MalayDisplay.Text = "malay10" Then
StartActivity("b")
Activity.finish
Else If MalayDisplay.Text = "malay11" Then
StartActivity("b")
Activity.finish

End If
 

mangojack

Well-Known Member
Licensed User
Longtime User
B4X:
Dim x As Int = MalayDisplay.text.SubString(5)

Select True
   Case x  >0 AND x < 5
     StartActivity("A")
   Case x > 7 AND x < 12
     StartActivity("B")
End Select

Note .. this is very restrictive as only works on words equal in length to 'malay'
 
Upvote 0

jai76

Member
Licensed User
Longtime User

Dear Mangojack,

Thanks for your reply,
how about if my string is various length?

for example :-

Sub Imv1_Click
If MalayDisplay.Text = "fire" Then
StartActivity("A")
Activity.finish
Else If MalayDisplay.Text = "box" Then
StartActivity("A")
Activity.finish
Else If MalayDisplay.Text = "switch" Then
StartActivity("A")
Activity.finish
Else If MalayDisplay.Text = "air" Then
StartActivity("A")
Activity.finish
Else If MalayDisplay.Text = "tractor" Then
StartActivity("b")
Activity.finish
Else If MalayDisplay.Text = "bulldozer" Then
StartActivity("b")
Activity.finish
Else If MalayDisplay.Text = "Police" Then
StartActivity("b")
Activity.finish
Else If MalayDisplay.Text = "apartment" Then
StartActivity("b")
Activity.finish

End If

Currently my total words is 2000, if i do link as above, thats mean 2000 x 3 = 6000. where B4A error when do compiling.

Really need help from all the members,
Thanks in advance..
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
Firstly .. Please use [Code] insert code here .. [/code] tags when posting code for easy reading..

I was aware my suggestion was very restrictive , It answered your original question and was something you could work on.

Your second post sample code is a completely different scenario and without knowing exactly what your trying to achieve ..
(Yes I understand you wish to create a Dictionary app) it is hard to help.

why do you wish to start a different activity dependent on the "word' ??
are your words located in a Database / text file ??

hopefully someone with more expertise in the dictionary app thingy can help you
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
As mangojack already said, without knowing what exactly you want to do it's impossible to give a concrete advice.
Where do your words come from ?
What algorythm defines the Activity to show ?
Why do you need or want to show different activities ?
etc.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
I think that your approach may be wrong. You should consider storing the words in a database. Your code will be much much smaller and you'll be able to use the power of SQL queries to find similar words. You may even be able to find a database with all the words already contained which would give you a real head start.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
Another thought, you could use a web service which might be even easier to code but will require a network connection for your app!
 
Upvote 0

Straker

Active Member
Licensed User
Longtime User
You have multiple solutions.
You can have fo example a map (https://www.b4x.com/android/help/collections.html#map) where your keys are the words ("fire", "box", "switc"...) and the value is something referred to the correct activity you wnat to open.

Example

B4X:
Sub Process_Globals
    Dim myMap As Map
End Sub
Sub Activity_Create(FirstTime As Boolean)
    myMap.Initialize
End Sub

Sub BuildMap
    myMap.Put("box","A")
    myMap.Put("fire","A")
    myMap.Put("police","B")
    '...
    'Probabily is better read all the pairs from a text file...
    'Mind thay "box" is different from "Box" therefore is better
    'write everything lowercase
End Sub

Sub CheckActivityToOpen   
    Dim ref As String = MalayDisplay.text.ToLowerCase
    Dim ActivityID As String = mymap.Get(ref)
    Select Case ActivityID
        Case "A" 'launch activity A
        Case "B" 'launch activity B
        '...other cases
        Case Else
            Msgbox("Is not in the map",ref)
    End Select
End Sub
 
Upvote 0

jai76

Member
Licensed User
Longtime User
Dear all,

Thanks for the advice
Honestly I'm a little bit confusing with the SQL, but i will tried it again.
thanks for all the information.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…