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

Discussion in 'Android Questions' started by jai76, Apr 25, 2015.

  1. jai76

    jai76 Member Licensed 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

    Code:
    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
     
  2. mangojack

    mangojack Well-Known Member Licensed User

    Code:
    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'
     
    jai76 likes this.
  3. jai76

    jai76 Member Licensed 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..
     
  4. mangojack

    mangojack Well-Known Member Licensed 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
     
  5. klaus

    klaus Expert Licensed 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.
     
  6. RandomCoder

    RandomCoder Well-Known Member Licensed 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.
     
  7. RandomCoder

    RandomCoder Well-Known Member Licensed 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!
     
  8. Straker

    Straker Active Member Licensed User

    You have multiple solutions.
    You can have fo example a map (http://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

    Code:
    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
     
    koaunglay likes this.
  9. jai76

    jai76 Member Licensed 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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice