Android Question How Show Sqllite Colmun in other activity ?

Alansari

Member
Licensed User
Longtime User
Hi Friends

I have fields of sqllite : id , name , age , note
and the name show in ListView in main activity

I want when i click in name in the listview jump to other activity to show all info in sql : name,age,note

Now i can show that in msgbox but i don't know how show that in other activity

How to do that ???

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: true
    #IncludeTitle: false
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

     Dim sqldb As SQL
   
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

  
    Dim Cur As Cursor
    Private List_V1 As ListView
    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("a_main")

    If File.Exists(File.DirDefaultExternal,"data1.db") = False Then
    File.Copy(File.DirAssets,"data1.db",File.DirDefaultExternal,"data1.db")
    End If
  
    sqldb.Initialize(File.DirDefaultExternal,"data1.db",True)
    Cur = sqldb.ExecQuery("SELECT * FROM t1")
  
    For i = 0 To Cur.RowCount -1
    Cur.Position = i
    List_V1.AddSingleLine(Cur.GetString("name"))
    Next
  

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    StartActivity("a_page")
  
End Sub
Sub List_V1_ItemClick (Position As Int, Value As Object)
    Cur = sqldb.ExecQuery("SELECT * FROM t1")
    Cur.Position = Position
    Msgbox("Name : "&Cur.GetString("name") & CRLF &"Age : " & Cur.GetString("age") & CRLF & "Note : " & Cur.GetString("note"),"")
  
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
What you need to transfer to the new activity is actually just an ID of the record, so you can make a new query with more info in the new activity. You can then transfer this ID as from Manfred with CallSub or CallSubDelayed.
 
Upvote 0

Alansari

Member
Licensed User
Longtime User
Thanks All

only i want the code for take ID to other activity when i click the name in listview

B4X:
Sub List_V1_ItemClick (Position As Int, Value As Object)
    Cur = sqldb.ExecQuery("SELECT * FROM t1")
    Cur.Position = Position
    Msgbox("Name : "&Cur.GetString("name") & CRLF &"Age : " & Cur.GetString("age") & CRLF & "Note : " & Cur.GetString("note"),"")

End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
only i want the code for take ID to other activity when i click the name in listview
In the main activity you can have this:
B4X:
Sub Process_Globals
    Dim Myid As Int
    Dim sqldb As SQL
End Sub

Sub List_V1_ItemClick (Position As Int, Value As Object)
    Dim Myname As String =Value
    Myid=sqldb.ExecQuerySingleResult2("SELECT id from t1 WHERE name =?",Array As String(Myname))
End Sub
In the second activity, you refer to the id as Main.Myid
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Sub List_V1_ItemClick (Position As Int, Value As Object)
Dim Myname As String =Value
Myid=sqldb.ExecQuerySingleResult2(
"SELECT id from t1 WHERE name =?",Array As String(Myname))
End Sub
The problem starts with the fact, that Value does not have a Content as he is using lv.AddSingleLine(...)

The should switch to AddSingleLine2 and add a Value in the first step
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
The problem starts with the fact, that Value does not have a Content as he is using lv.AddSingleLine(...)
The should switch to AddSingleLine2 and add a Value in the first step

He does not need to change to AddSingleLine2. It should work because he loads up the listview with the name field in his code:
List_V1.AddSingleLine(Cur.GetString("name"))
So the item clicked in the listview will return the entire value which is a name.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
So the item clicked in the listview will return the entire value which is a name.
You are right! It returns the hole string as Value. I was not aware of that. Ok, never tried it :D
I was in the thought that i need to use the AddSingleLine2 command... So i aways use this variant
 
Upvote 0

Alansari

Member
Licensed User
Longtime User
In the main activity you can have this:
B4X:
Sub Process_Globals
    Dim Myid As Int
    Dim sqldb As SQL
End Sub

Sub List_V1_ItemClick (Position As Int, Value As Object)
    Dim Myname As String =Value
    Myid=sqldb.ExecQuerySingleResult2("SELECT id from t1 WHERE name =?",Array As String(Myname))
End Sub
In the second activity, you refer to the id as Main.Myid

Nice Bro

Its Done

Thanks
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
A few tips:

1. The cursor should be a local variable and it should be closed when done.
2. You should probably move the SQL object to the starter service.
3. Never use Dim in Process_Globals. Use Private or Public.
4. If the SQL variable is public then it must be moved to the starter service and initialized there.
5. Don't use Msgbox. Use MsgboxAsync instead.
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
A few tips:

1. The cursor should be a local variable and it should be closed when done.
2. You should probably move the SQL object to the starter service.
3. Never use Dim in Process_Globals. Use Private or Public.
4. If the SQL variable is public then it must be moved to the starter service and initialized there.
5. Don't use Msgbox. Use MsgboxAsync instead.
How do I make an async msgbox to wait in b4a?
 
Upvote 0
Top