Android Question Using the same Type structure in more than one Activity

Mark Stuart

Active Member
Licensed User
Longtime User
Hi all,

This pertains to using an Activity and not B4XPages, which I don't use.

I have a Type structure defined in the Main Activity and need to use it in another Activity called AddItem.
How do I reference the Main Type structure in the AddItem Activity?

Regards,
Mark Stuart
 

JohnC

Expert
Licensed User
Longtime User
Just move the Type structure to the "Process_Globals" section of a Code module and reference it as CodeModuleName.TypeStructureName from both activities.
 
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
Thank you John as I searched the forum and didn't find a mention on the use of a Type definition and its multi use. So thank you again.
Mark
 
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
Just move the Type structure to the "Process_Globals" section of a Code module and reference it as CodeModuleName.TypeStructureName from both activities.
Got to the computer and created the Code module and copied the Type into the Process_Globals as:
Type Item (Name As String, Country As String, Phone As String, Email As String)

In the Main activity, in the Process_Globals I started typing the reference to the Type in the code module - it wouldn't reference even the code module name and obviously therefore couldn't see the Process_Globals I created there.

It doesn't take the Public word before the Type either.
I don't use Types at all in my apps, so I'm new to using them.

What am I doing wrong?
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
To use a Type, you need to DIM a variable as that type:

B4X:
(CodeModule)
Sub Process_Globals
     Type ItemType (Name As String, Country As String, Phone As String, Email As String)
End Sub

(Activity Code)
Dim MyItem as CodeModuleName.ItemType

MyItem.Name = "John"
MyItem.Country = "US"
MyItem.Phone = "123-456-7890"
MyItem.Email = "test@test.com"
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Where in the Main module is the Dim placed?
You can place the DIM line in a Process_Globals, Globals or any other sub depending how much scope you want that variable to have.
 
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
Well it seems I've figured it out with placing the Type in a Code module and then referencing the Type in my code as the following.I don't have to "re-declare" the Type in the Main Activity module as you've suggested.
Here's the code in use in the Main Activity module:
B4X:
Sub LoadList
    clvItems.Clear
    For Each it As Item In AppTypes.SavedListItems    '<-- notice the Item. This is the Code module declaration Type.
        Dim pnl As B4XView = CreateItemPanel(it)
        clvItems.Add(pnl, it)
    Next
End Sub

B4X:
Sub Process_Globals
    ' Extended structured Type
    Type Item (Name As String, Country As String, Phone As String, Email As String)
    Public SavedListItems As List
End Sub

John, is this what and how you were referring to the structure and use of the Type?
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
John, is this what and how you were referring to the structure and use of the Type?
Yes, as I mentioned you can place the DIM (or "public") in the Process_Globals sub.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Once you declare the Type in a Process_Globals, you don't have to (and can't) do it elsewhere; you can create variables of that type (Dim... and Initilalize) anywhere.

The best place to declare the Type is in the Main Activity (or in the B4XMainPage), as it's mandatory to have it there if you want(ed) to serialize (B4XSerializator) variables of that type.

B4XPages, which I don't use.
This is not a good idea, at all.
 
Last edited:
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
No, I don't think I'll be going to b4xpages. Nothing there is a benefit to me or is required to using b4xpages in my apps. I'm fine where I am.
And I don't like ppl saying its a mistake not to use b4xpages. Not appropriate in a forum.

In the past looking at the 3 pages example, I can't believe how you have to manage each page declaration and show of it. What a waste of functionality and coding time.
Just imagine an app with lots of activities/pages. What a nightmare!
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
And I don't like ppl saying its a mistake not to use b4xpages. Not appropriate in a forum.
So from now on (or before? 😄) you don't like me anymore, but I didn't write that it's a mistake but that it's not a good idea:
This is not a good idea, at all.


Nothing there is a benefit to me or is required to using b4xpages in my apps
There are at least those 20 good reasons (tutorial) to choose B4XPages projects; perhaps the descriptions don't convey the idea as well as practice would.

I'm fine where I am
And I'm happy for you.
I'm sorry you're not switching to B4XPages, because for every member I convince to use them, I get €300... from the Pope😄

Of course, I mean, I wouldn't have any advantage in convincing you. I highly recommend it because the benefits are so many and useful, but if you don't believe me... that's okay, I like you anyway😊
And I don't like ppl saying its a mistake not to use b4xpages
 
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
Based on forum standards I guess I should be saying to you: please start a new thread on this and continue your discussion there. 😄
 
Last edited:
Upvote 0
Top