Android Question How to pass RowID

aklisiewicz

Active Member
Licensed User
Longtime User
I have following fragment of code, where I need to pass the ID field (Row ID) from ListView to another activity



(which displays the record details as form). How would I read and pass ID column ?

B4X:
Sub Activity_Create(FirstTime As Boolean)
    SQL1.Initialize(Main.dbFileDir, Main.dbFileName, True)
    Activity.LoadLayout("DiseasesListView")
    QueryString = "SELECT DiseaseID, ComonName, ComonSubname FROM "& dbTableName &" ORDER BY ComonName"
    Activity.Title = "DISEASES"
    FillListView
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub ListViewInit
    ListView1.SingleLineLayout.ItemHeight = 40dip
    ListView1.SingleLineLayout.Label.Left = 0
    ListView1.SingleLineLayout.Label.Width = 100%x
    ListView1.SingleLineLayout.Label.Height = 40dip
    ListView1.SingleLineLayout.Label.Gravity = Gravity.CENTER_VERTICAL
    ListView1.SingleLineLayout.Label.Color = Colors.White
    ListView1.SingleLineLayout.Label.TextSize = 12
      ListView1.SingleLineLayout.Label.TextColor = Colors.Black
    ListView1.FastScrollEnabled = True
End Sub

Sub FillListView
    ListView1.Clear
    Cur = SQL1.ExecQuery(QueryString)
    For i = 0 To Cur.RowCount - 1
        Cur.Position = i
        ListView1.AddSingleLine(Cur.GetString("ComonName"))
    Next
'    cur.Close
End Sub

Sub ListView1_ItemClick (Position As Int, Value As Object)
    StartActivity(DiseaseFormView)
End Sub


I want to use regular RowClick event to start
B4X:
 StartActivity(DiseaseFormView)


The table structure looks like this
B4X:
CREATE TABLE "dDISEASES" ("DiseaseID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,"ComonName" CHAR(80)  NOT NULL ,"ComonSubname" CHAR(40)  NOT NULL ,"MedicalName" CHAR(80)  NOT NULL ,"LatinName" CHAR(80)  NOT NULL ,"OtherName" CHAR(80)  NOT NULL ,"Description" TEXT  NOT NULL ,"Falg" SMALLINT  NOT NULL ,"Locked" SMALLINT  NOT NULL ,"lang" CHAR(2)  NOT NULL ,"Operator" INT  NOT NULL , "LastUpdate" DATETIME)

Arthur
 

derez

Expert
Licensed User
Longtime User
You can use a process_global variable to store the item's value. After selection of the item assign its value to the variable and DiseaseFormView should read this value from the global variable and do the rest.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub Process_Globals
   Dim selectedDiseaseID="" As String
End Sub

Sub FillListView
    ListView1.Clear
    Cur = SQL1.ExecQuery(QueryString)
    For i = 0 To Cur.RowCount - 1
        Cur.Position = i
        ListView1.AddSingleLine2(Cur.GetString("ComonName"), Cur.GetString("DiseaseID"))
    Next
'    cur.Close
End Sub

Sub ListView1_ItemClick (Position AsInt, Value AsObject)
  selectedDiseaseID = Value  
  StartActivity(DiseaseFormView)
End Sub
 
Upvote 0

aklisiewicz

Active Member
Licensed User
Longtime User
Thank you all for your input...!

now it is almost clear,... but here I have additional questions:

1. from what I know passing values suing global variables is not the best idea for any type of programing. Is there a way to pass value the way we pass value to the subroutine ?

2.What is the difference between
B4X:
ListView1.AddSingleLine2(Cur.GetString("ComonName"), Cur.GetString("DiseaseID"))

and: AddSingleLine ?

3. How can I hihlight selected record and memorize it so when the user clicks on BACK button he will see the same record still highlighted ?

4. DiseaseID is declared as INT, and you advising to pass value as String not as Integer so how would I read this into a query which will select appropriate record on the Form (in called Activity) ?
 
Last edited:
Upvote 0

aklisiewicz

Active Member
Licensed User
Longtime User
I'm a bit confused about this:
B4X:
selectedDiseaseID = Value
DonManfred - how did you get this "Value" here ?
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
1.
B4X:
Sub ListView1_ItemClick (Position AsInt, Value AsObject)
  selectedDiseaseID = Value
  'StartActivity(DiseaseFormView)
  CallSubDelayed2(DiseaseFormView, "SubName", selectedDiseaseID)
End Sub

2.
Last parameter, is the value "returned" when select that item.

3.
I don't know (but i remember a similar question, so search it on site)
P.S. try this

4.
If you are referring to:
B4X:
selectedDiseaseID = Value

Value will converted to Int
 
Last edited:
Upvote 0

aklisiewicz

Active Member
Licensed User
Longtime User
OK, so which Sub can I call (when I'm trying to call like this:
B4X:
CallSubDelayed2(DiseaseFormView, "Activity_Create", selectedDiseaseID)
I get error:
java.lang.Exception: Sub activity_create signature does not match expected signature.

also, can I make an assignment like this:
B4X:
QueryString = "SELECT * FROM dDISEASES WHERE DiseaseID =" & selectedDiseaseID
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
2.What is the difference between
ListView1.AddSingleLine2(Cur.GetString("ComonName"), Cur.GetString("DiseaseID"))
and: AddSingleLine ?

AddSingleLine ads a single line like AddSingleLine2 but with the last you setup a RETURN-VALUE too. The last Parameter "Cur.GetString("DiseaseID")" is that what you get if you click on an item...

B4X:
Sub ListView1_ItemClick (Position AsInt, Value AsObject)
  selectedDiseaseID = Value ' The value given in this sub is the second parameter of AddSingleLine2
  'StartActivity(DiseaseFormView)
  CallSubDelayed2(DiseaseFormView, "SubName", selectedDiseaseID)
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
also, can I make an assignment like this:
B4X:
QueryString = "SELECT * FROM dDISEASES WHERE DiseaseID =" & selectedDiseaseID

try this in

B4X:
Sub ListView1_ItemClick (Position AsInt, Value AsObject)
 selectedDiseaseID = Value ' The value given in this sub is the second parameter of AddSingleLine2
 StartActivity(DiseaseFormView)
End Sub

and in Activity_create of your DiseaseFormView you can use the process_global variable selectedDiseaseID so you do not need to give it as parameter...

B4X:
QueryString = "SELECT * FROM dDISEASES WHERE DiseaseID =" & main.selectedDiseaseID
 
Upvote 0

aklisiewicz

Active Member
Licensed User
Longtime User
OK, never mind I got it....all above, but there are new questions

I have a field in database called DESCRIPTION
Obviously it is a text type column so it can gold many lines of text. When I display the text there is only several lines displayed and the rest is hidden below. What is the best method to make it scrollable ?






Thank you all for support - Arthur
 
Last edited:
Upvote 0
Top