Android Question A class for database operations

JaunLukePicard

Member
Licensed User
Longtime User
I know from an Activity you can reference a class that has functions that may be utilized by other Activities as well (re-usable) We have to reference the class and then initialize to be able to gain access to the variables and subs within it.

Can I have?

Activity called "Editor" to work with a record from the database via a Layout.

A class called "clsEditor" related specifically to the Editor which allows for Add/Edit/Update/Delete for a specific table/database. (related to the Editor)

A separate "Database" class that has generic operations


The user would call up the Editor which would use the "Database" class for generic operations (that may be accessible by all Activities that utilize general database operations. In the case where the user does CRUD operations then the clsEditor class would be used.

In some cases I'd like to make calls from clsEditor to Database or vice-versa. Is this possible in B4A or would I just have to call the classes only from the Activity itself. Could I pass variables between them (i.e. oModule which would get the reference of "Me" when the class is initialized.

I know I can do this in VisualStudio, but can I do it in Basic4Android. My reason is to segment off the code to each module based upon its purpose. This will reduce the amount of coding required between Activities as CRUD and other DB operations are somewhat similar except for the table structure.
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Yes - you can have an activity that has its own class to handle the back-end processing. For example, you can have an Options activity & a clsOptions class. When you start the activity, you can create an instance of clsOptions to load the app settings from a file & put them into the relevant views in the Activity. The beauty of this is that you can also use the same clsOptions class to get the settings you need at the start of your Main activity. If you want a single instance of your class that you can reference anywhere in the app, then you can just create it in Process_Globals in your Main activity. Then you can access it from any other activity or module by referring to Main.instance.

- Colin.
 
Upvote 0

JaunLukePicard

Member
Licensed User
Longtime User
Colin,

OK. Got that and I do have one class like this, clsTable for a ScrollView2d table., Very handy.

Here is an example of what I want to do next:

I have an Editor Activity which allows me to have an Editor class, clsEditor which was defined in the Editor Activity. I also want a second class that handles the database operations for the Editor Activity and can be accessed from the clsEditor class. So in this case I would have two classes:

Like...
actEditor The Editor form
clsEditor The class for Form actions
clsDBEditor The class for database operations specific to actEditor and clsEditor


Given the above how would I have both classes refer to each other. Would they communicate through actEditor? Would I use "Me" when initializing that so I could have each refer to the other and the Activity?

How would I go about this if you would have an example. I am close now, but I get an error indicating that the clsDBEditor wasn't initialized (the class that handles the database operations).

Peter
:rolleyes:
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
OK... If I understand this correctly, it sounds like you want the activity & 2 classes to be referencing the same instances of the classes (if you can follow that!) - in which case you should create an instance of each class in the Process_Globals of the activity then use it in your classes by referring to <activity.class>. Let's see if I can illustrate this:

B4X:
'=================
'actEditor Activity
-=================

Sub Process_Globals
    Public cEditor as clsEditor
    Public cDBEditor as clsDBEditor
End Sub

Sub Activity_Create(FirstTime as Boolean)
    cEditor.Initialize
    cDBEditor.Initialize
End Sub

'=====================
'clsEditor Class
'=====================
Public Sub Initialize
    actEditor.cDBEditor.DoSomething
End Sub

'=================
'clsDBEditor Class
'================
Public Sub Initialize
    actEditor.cEditor.DoSomething
End Sub

Not a great example, but you get the idea! When actEditor is finished, the global references to the 2 classes (the instances) will be destroyed.
 
Upvote 0

JaunLukePicard

Member
Licensed User
Longtime User
Colin,

The example you showed me worked like a charm. I was declaring the classes in Globals which was the reason they could not be seen.

I do have one related question to this and that is...

When you define a variable in Globals it is Private. If you define the variable in Process_Globals it is Public.

In the case of my classes above if I want to create a pointer in the oDBEditor to track the current record position can I access this from the other classes of the Activity OR do I need to define the variable in the Activity itself?
 
Upvote 0

JaunLukePicard

Member
Licensed User
Longtime User
I had figured it out and then wrote a select statement as the following code shows...
B4X:
    Dim iCount As Int
    Dim iIndex As Int
    iCount = actStoreEditor.oclsDatabase.fncRecordCount
    iIndex = actStoreEditor.oclsDatabase.SelectedRecIndex
    If iCount > 0 Then
        Select iIndex
            Case Is = 0
            Case Is > 0
            Case Else
        End Select
    End If

In the above block of code I get an error on iIndex in the Select line. The message says "Missing parameter". Would you know what this is referring to. I used this method as opposed to placing the code that calls for the SelectedRecIndex on the Select line thinking that that would fix the error. ell it didn't.

Any ideas please?
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
I had figured it out and then wrote a select statement as the following code shows...
B4X:
    Dim iCount As Int
    Dim iIndex As Int
    iCount = actStoreEditor.oclsDatabase.fncRecordCount
    iIndex = actStoreEditor.oclsDatabase.SelectedRecIndex
    If iCount > 0 Then
        Select iIndex
            Case Is = 0
            Case Is > 0
            Case Else
        End Select
    End If

In the above block of code I get an error on iIndex in the Select line. The message says "Missing parameter". Would you know what this is referring to. I used this method as opposed to placing the code that calls for the SelectedRecIndex on the Select line thinking that that would fix the error. ell it didn't.

Any ideas please?
Yeah - I was going to suggest that you create a property in the class & use that to store & retrieve your record index. Given that you have done just that, I'm assuming you sorted out your getters & setters.

With your Select statement, I don't think you can use operators the way you are using them. It's probably easier to use an If statement:

B4X:
If iIndex = 0 Then
    DoSomething
Else If iIndex > 0 Then
    DoSomethingElse
Else
    DoSomethingElseElse
End If

But if you really want to use a Select statement, you could do this:

B4X:
Select Case True 'I always use "Case" even though Erel says it's optional
    Case (iIndex = 0)
        DoSomething
    Case (iIndex > 0)
        DoSomethingElse
    Case Else
        DoSomethingElseElse
End Select

Hope this helps.

- Colin
 
Upvote 0

JaunLukePicard

Member
Licensed User
Longtime User
Colin,

I see your point. I still prefer to use Select Case as it appears to be a bit cleaner. Sometimes when you have a lot of IF ELSEIF statements it looks a bit confusing. Especially when you've been away from the code for a while and come back to it.

Personal preference really.

Thank you so much. it WORKED!
:p
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Colin,

I see your point. I still prefer to use Select Case as it appears to be a bit cleaner. Sometimes when you have a lot of IF ELSEIF statements it looks a bit confusing. Especially when you've been away from the code for a while and come back to it.

Personal preference really.

Thank you so much. it WORKED!
:p
Glad I could be of some help.

- Colin.
 
Upvote 0
Top