Android Question Passing several value to another activity

Discussion in 'Android Questions' started by fasilosman, Jun 23, 2015.

  1. fasilosman

    fasilosman Member Licensed User

    HI,

    I don't the exact command to pass more than one values/parameters to another activity.

    I use "CallSubDelayed2" two times to pass 2 values.

    But the targeted activity is created two times.

    Can any body advice me in this regard.

    Thank you
     
  2. DonManfred

    DonManfred Expert Licensed User

    Use a MAP to store all Values you need to Transport.
    Use this map in the callsubdelayed2 call as value
     
  3. fasilosman

    fasilosman Member Licensed User

  4. Erel

    Erel Administrator Staff Member Licensed User

    Example:
    Code:
    CallSubDelayed2(Activity2, "SomeSub", CreateMap ("color"Colors.Red, "width"200))
     
    RandomCoder and lemonisdead like this.
  5. LucaMs

    LucaMs Expert Licensed User

    No one likes the classes.
    I understand that in some cases it's easier to use a Map (I love the Maps) or an array, but... there is little talk of classes, in this site.
    Yet, "severl values", often means a precise object.

    (A little bit off topic, I know; but at least I'll add a post in which you talk about classes :D)
     
  6. fasilosman

    fasilosman Member Licensed User


    Can I have an example on how to use the map on activity2
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    Public Sub SomeSub (Params As Map)
     
    Dim clr As Int = Params.Get("color")
    End Sub
     
  8. fasilosman

    fasilosman Member Licensed User

    thanks
     
  9. SteveTerrell

    SteveTerrell Active Member Licensed User

    Interesting, that would be good for many of my applications.

    Is a class passed by reference?
     
  10. DonManfred

    DonManfred Expert Licensed User

    you cannot pass a reference to an class-instance to another activity. Each activity can - or need to use - an own instance of the class.

    Classes are not a good idea here to pass values from activity A to B
     
  11. SteveTerrell

    SteveTerrell Active Member Licensed User

    Looks possible in a single activity though where different instances of the same class can be passed as parameters to a sub. From a quick timing check it looks like it is by reference.
     
  12. LucaMs

    LucaMs Expert Licensed User

    I see no logical things, here.

    I'm attaching a test project.

    In Test 1 I pass an object of type AHLocale from Main to SecondActivity through a CallSubDelayed2; I get this error:
    java.lang.Exception: Sub setlocale signature does not match expected signature.


    In Test 2 I try to pass an object created from a my class and I get a different error:
    java.lang.ClassCastException: java.lang.Object[] cannot be cast to b4a.example.myclass



    However, this is an absurd limitation, never seen in other languages.

    Classes in B4A, at this point, do not work as they should.
     

    Attached Files:

    Last edited: Jun 23, 2015
  13. Erel

    Erel Administrator Staff Member Licensed User

    I think that all languages treat arrays as arrays and non-arrays as non-arrays.

    Your code is wrong. You shouldn't pass an array if the sub expects a non-array parameter.
     
  14. LucaMs

    LucaMs Expert Licensed User

    Yes, that's was the error in Test 1, but my comments about "other languages" concerns classes/objects.
    In fact, I do not get errors now from test 1, passing an object to the routine of SecondActivity (Local object), but I can not pass an object created from my class.


    Code:
    Public Sub SetMyClassObject(MyClassObject As Object) ' <--- this does not work
    'Public Sub SetMyClassObject(MyClassObject As MyClass)  ' <--- this does not work
        Dim MyObject As MyClass = MyClassObject
        
    Log(MyObject.Name)
    End Sub

    Public Sub SetLocale(Loc As AHLocale' <---  this works, Loc is an object
        Log(Loc.Country)
    End Sub
     
    Last edited: Jun 23, 2015
  15. Erel

    Erel Administrator Staff Member Licensed User

    Works here fine after I fixed your code and removed the array.
     
    LucaMs likes this.
  16. LucaMs

    LucaMs Expert Licensed User

    I had already fixed the error (array) in the project attached in #12.

    But neither of these declarations work:
    Code:
    Public Sub SetMyClassObject(MyClassObject As Object) ' <--- this does not work
    'Public Sub SetMyClassObject(MyClassObject As MyClass)  ' <--- this does not work
        Dim MyObject As MyClass = MyClassObject
        
    Log(MyObject.Name)
    End Sub
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    Please upload a new project with the update code.
     
  18. LucaMs

    LucaMs Expert Licensed User

     

    Attached Files:

  19. Erel

    Erel Administrator Staff Member Licensed User

    Luca you are still passing an array instead of a single object:

    [​IMG]
     
  20. LucaMs

    LucaMs Expert Licensed User

    Ehm... :rolleyes:

    I changed the array parameter only in Test 1.

    Now it works
     
Loading...