Android Question Creating a SQL query using input from multiple activities?

gareththomasnz

Member
Licensed User
Longtime User
I am making a CRUD app for a client

He has asked that I have input fields across multiple screens

The user inputs to a couple of fields - hits the next button and proceeds to the next screen

In all there are 7 input screens and a save to database button on the last screen

I have put each screen into its own activity so the user can use the back button of the device

The add (to database) is a sub on the first activity - I can use callsub on the last activity to call this

But I am thinking if each screen (activity) has some input of the fields how do I get the add sub in the first activity to get the field data from those other activities?

B4X:
Sub AddEntry

    Dim Query As String

Query = "INSERT INTO User VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
        SQL1.ExecNonQuery2(Query, Array As String(rdbMr.Checked , rdbMrs.Checked, rdbMiss.Checked, rdbElecInsp.Checked, rdbSparky.Checked, _
        rdbTech.Checked, rdbOther.Checked, txtFirstName.Text, txtMidName.Text, txtLastName.Text, txtPersEmail.Text, _
        txtPerPh.Text, txtPerCell.Text, txtPerWork.Text, txtStreet.Text, txtCity.Text, txtSuburb.Text, txtRego.Text, txtEmpComp.Text, _
        txtCompAddress.Text, txtEmpTown.Text, txtContact.Text,txtCompPhone.Text, txtCompEmail.Text, rdbNN.Checked, rdbCR.Checked, txtSiteSf.Text))
     
        ToastMessageShow("Entry added", False)    ' confirmation for the user

End Sub

I am just looking in the following for an answer:

https://www.b4x.com/android/forum/threads/passing-a-parameter-from-activity-a-b.9763/

https://www.b4x.com/android/forum/threads/two-activities-example.6611/#post38609


https://www.b4x.com/android/forum/threads/android-process-and-activities-life-cycle.6487/#post37980
 
Last edited:

Mahares

Expert
Licensed User
Longtime User
One way I would do it is:
1. Declare Process-Global variables in the main activity: exple: Dim Street as String
2. If txtStreet.Text is input in the 3rd activity, you can have : Main.Street =txtStreet.Text
3. Finally, when you are ready to save the SQLite record in the main activity, you use this in the query: Main.Street instead of txtStreet.Text
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

gareththomasnz

Member
Licensed User
Longtime User
The save button is in activity eight & the actual sql sub is in activity one

I just tried CallSubDelayed and lucky for me that works as the screen goes back to activity 1 upon saveing

OK so I know that works - the next problem for me is checking if the data is actually added correctly to the database

It is creating the entry in the database - BUT - only the data from the first activity is going into the database

All the other fields from the other activities are not passing through - is this because the activities are paused?

I am using the code as suggested in the post above.

Should the following:

Main.Street =txtStreet.Text

Go into Sub Process Globals, Sub Globals or Sub Activity_Create?

I stuck it into Sub Activity_Create which creates no error but wont load up the DB

So I will experiment...
 
Last edited:
Upvote 0

gareththomasnz

Member
Licensed User
Longtime User
OK fingers crossed I was just going to try that but backed out LOL - I have it adding the row to the database - just most of the cells didn't load data yet

Hopefully this will work

Thanks


It needs simply to load the data into the database & go back to screen one after pressing
the save button - and show the toast message

So it is all there just not writing to the DB

Help much appreciated if you can explain what is wrong
 
Last edited:
Upvote 0

gareththomasnz

Member
Licensed User
Longtime User
Admin Password is "applepie"

PS it only created the new row with the Main.Street = txtStreet.text in the sub oncreate in the other modules

this version has it in the on pause & it doesn't even load the new row
 
Last edited:
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
Hey Gareth,
I think, theonly problem is in your PanelNine Activity in the Sub Activity_Create when you check your Input as admin.
here you are deleting the database edited before and copy the original database from assets, so the input data will be deleted!
'Put this to comment and it will work!

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
   
    If FirstTime Then
       File.Delete(File.DirInternal, "electapp.db") ' <- this produces the error described above...
       
        'check if the database already exists
        If File.Exists(File.DirInternal, "electapp.db") = False Then
            'copy the default DB
            File.Copy(File.DirAssets, "electapp.db", File.DirInternal, "electapp.db")
            'if not, initialize it
            Main.SQL1.Initialize(File.DirInternal, "electapp.db", True)
            'and create it
            'CreateDataBase
            'copy the default DB
            File.Copy(File.DirAssets, "electapp.db", File.DirInternal, "electapp.db")
        Else
            'if yes, initialize it
            Main.SQL1.Initialize(File.DirInternal, "electapp.db", True)
        End If
    End If   
   
    Activity.LoadLayout("blackandwhite")
....
End Sub
 
Upvote 0

gareththomasnz

Member
Licensed User
Longtime User
Thanks for the reply

Yes I believe with that commented out it did load the new row of data but that row only passed the data from the first panel all the rest was empty

So this is not the only fault (although it clearly is a fault).

Also Erel says Klaus is correct & that it should indeed be in Activity_Pause. It may be that I uncommented the panel nine block while testing that?

Update:

Yes thank heavens it is working now.

It was a combination of the code on panel nine that needed to be commented or removed and the other code that needed to be placed into Activity_Pause.

Phew!!
 
Last edited:
Upvote 0
Top