Android Question listview values / queries

merlin2049er

Well-Known Member
Licensed User
Longtime User
I wanted to pass either the value or position from the listview to a click event which runs a SQL query.

I can't use the value with my existing SQL query. I think I'd have to write a new SQL statement using the listview position, but that's not necessarily the index of my table.

So, how could I setup the listview with my table index initially. I think this will solve my problem.
Then I can pull up the record based on the table ID.
 

LucaMs

Expert
Licensed User
Longtime User
I wanted to pass either the value or position from the listview to a click event which runs a SQL query.

I can't use the value with my existing SQL query. I think I'd have to write a new SQL statement using the listview position, but that's not necessarily the index of my table.

So, how could I setup the listview with my table index initially. I think this will solve my problem.
Then I can pull up the record based on the table ID.



You can use DBUtils.ExecuteListView
(next time, post your query SQL)
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
merlin .. Are you using DBUtils ..? if so I cannot help.

in one app I populate the listview with the .AddTwoLines2 method .. that allows you to set a return value for the click event. I use the table ID as that value .

B4X:
'Populate the ListView with DB data setting the Record ID as return value ...
 
Cursor1 = SQL1.ExecQuery("SELECT * FROM MyTable")
  
   For i = 0 To Cursor1.RowCount - 1
     Cursor1.Position = i
     ID = Cursor1.GetInt("ID")
     Fname = Cursor1.GetString("FirstName")
     Lname = Cursor1.GetString("LastName")
  
     ListView.AddTwoLines2(Fname ,Lname ,ID)
   Next

Cheers mj
 
Last edited:
Upvote 0

merlin2049er

Well-Known Member
Licensed User
Longtime User
I got the listview populated from a query and put it into 2 lines of listview data & a pic. Now, I'd like to drill down on that listview and run a query which selects that record.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
So, how could I setup the listview with my table index initially. I think this will solve my problem.
Then I can pull up the record based on the table ID.

have a look at ListView.AddTwoLinesAndBitmap2 then ... when you populate the list with your data and image .. the last supplied value is the table records ID which will be the return value on an List click event .. use that return value(table ID) to run your subsequent query.

B4X:
Sub ListView_ItemClick (Position As Int, Value As Object)
   dim recID as String = Value
  
   Cursor1 = SQL1.ExecQuery("SELECT * FROM MyTable WHERE ID = '"& recID &"' ")

edit ... as only one record should exist I think you can use ..
B4X:
   Cursor1 = SQL1.ExecSingleQuery("SELECT * FROM MyTable WHERE ID = '"& recID &"' ")

Cheers mj
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
In that case, mangojack, you need to query the db every time.

This is the "help" (intellisense) of DBUtils.ExecuteListView command:

upload_2013-12-5_5-16-22.png


This is the link to download DBUtils
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
In that case, mangojack, you need to query the db every time.
That is correct .. every time the list is clicked .. which is what I understood merlin wished to do.

I wanted to pass either the value or position from the listview to a click event which runs a SQL query.

So, how could I setup the listview with my table index initially. I think this will solve my problem.
Then I can pull up the record based on the table ID.

is DBUtils absolutely necessary to achieve this .? or am I misunderstanding merlins problem ..

cheers mj
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
LucasMS .. no problems .. Even knowing English I also have misunderstood other forum members problems ;)

Cheers mj
 
Upvote 0

merlin2049er

Well-Known Member
Licensed User
Longtime User
merlin .. Are you using DBUtils ..? if so I cannot help.

in one app I populate the listview with the .AddTwoLines2 method .. that allows you to set a return value for the click event. I use the table ID as that value .

B4X:
'Populate the ListView with DB data setting the Record ID as return value ...

Cursor1 = SQL1.ExecQuery("SELECT * FROM MyTable")
 
   For i = 0 To Cursor1.RowCount - 1
     Cursor1.Position = i
     ID = Cursor1.GetInt("ID")
     Fname = Cursor1.GetString("FirstName")
     Lname = Cursor1.GetString("LastName")
 
     ListView.AddTwoLines2(Fname ,Lname ,ID)
   Next

Cheers mj


This looks like it should help me. I'd like to hide the table id in this list I'm building, and need to extract that ID when I click on that particular item in the list view.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
If you still want to use ListView, you can ListView2 and store the id in the tag part of the ListView. This is what I do for my Apps.

:)

ETA : oops, sorry something like this :

ListView1.AddTwoLines2(Cursor.GetString("txt1"), Cursor.GetString("txt2"), Cursor.GetInt("_id"))
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Merlin .. Have you solved your problem ?

Are you using ListView.AddTwoLinesAndBitmap ? .. if so Use ListView.AddTwoLinesAndBitMap2
The last parameter / argument supplied to this method is the Records ID number.
It is Not Displayed in the List , It is returned as the value on a ListView Click Event.
You can then use this returned value (ID) to do a Query on that table ID recordset.

B4X:
Sub ListView1_ItemClick (Position As Int, Value As Object)
  dim recID as String = Value

Cursor1 = SQL1.ExecSingleQuery("SELECT * FROM MyTable WHERE ID = '"& recID &"' ")

Cheers mj
 
Upvote 0
Top