Android Question displaying data from to all customlistveiw rows

aligol

Member
I need your help

I'v created ten item for customlistveiw and i'v three lable in each item now I intended to attribute some data from my database to each lable of my items but when i test my code in emulato only my item number ten shows data and rest of 9 items are empty!! here is my codes, what is my mistake and how i can show data to rest of items?


Sub Globals

'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim rs As ResultSet
Dim cur As Cursor
Dim sql1 As SQL
Dim jo As JavaObject
Private clv_item As CustomListView
Private Label2 As Label
Private Label1 As Label
Private Label3 As Label

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("L_clv")

Dim xui As XUI

For i= 1 To 10
Dim br As B4XView= xui.CreatePanel("l")
br.SetLayoutAnimated(100,0,0,100%x,50dip)
br.LoadLayout("l_items")
clv_item.Add(br,"")
Next
If sql1.IsInitialized=False Then
sql1.Initialize(File.DirInternal,"pro6.db",False)
End If

cur=sql1.ExecQuery("SELECT * FROM tblvoc where id in (1,2,3,4.5)")
For i=1 To cur.RowCount-1
cur.Position=i
Label1.Text=cur.GetString("dif")
Label2.Text=cur.GetString("voc")
Label3.Text=cur.GetString("meaning")
Label3.TextColor=Colors.Red
jo.InitializeContext.RunMethod("zxc",Array As Object(Label1))
Next

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

#if java
import android.widget.TextView;
import android.text.method.ScrollingMovementMethod;
public void zxc (TextView x) {x.setMovementMethod(new ScrollingMovementMethod());}
#end if
 

Sagenut

Expert
Licensed User
Longtime User
I know very few about SQL DB but try this

B4X:
If sql1.IsInitialized=False Then
    sql1.Initialize(File.DirInternal,"pro6.db",False)
End If

cur=sql1.ExecQuery("SELECT * FROM tblvoc where id in (1,2,3,4.5)")

For i= 1 To 10
    Dim br As B4XView= xui.CreatePanel("l")
    br.SetLayoutAnimated(100,0,0,100%x,50dip)
    br.LoadLayout("l_items")
    cur.Position=i
    Label1.Text=cur.GetString("dif")
    Label2.Text=cur.GetString("voc")
    Label3.Text=cur.GetString("meaning")
    Label3.TextColor=Colors.Red
    jo.InitializeContext.RunMethod("zxc",Array As Object(Label1))
    clv_item.Add(br,"")
Next
Anyway I think that you should not use Cursor nowadays but only ResultSet.
Someone more expert in DB will help you further.
I don't even know if my code will work.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

aligol

Member
I know very few about SQL DB but try this

B4X:
If sql1.IsInitialized=False Then
    sql1.Initialize(File.DirInternal,"pro6.db",False)
End If

cur=sql1.ExecQuery("SELECT * FROM tblvoc where id in (1,2,3,4.5)")

For i= 1 To 10
    Dim br As B4XView= xui.CreatePanel("l")
    br.SetLayoutAnimated(100,0,0,100%x,50dip)
    br.LoadLayout("l_items")
    cur.Position=i
    Label1.Text=cur.GetString("dif")
    Label2.Text=cur.GetString("voc")
    Label3.Text=cur.GetString("meaning")
    Label3.TextColor=Colors.Red
    jo.InitializeContext.RunMethod("zxc",Array As Object(Label1))
    clv_item.Add(br,"")
Next
Anyway I think that you should not use Cursor nowadays but only ResultSet.
Someone more expert in DB will help you further.
I don't even know if my code will work.
I know very few about SQL DB but try this

B4X:
If sql1.IsInitialized=False Then
    sql1.Initialize(File.DirInternal,"pro6.db",False)
End If

cur=sql1.ExecQuery("SELECT * FROM tblvoc where id in (1,2,3,4.5)")

For i= 1 To 10
    Dim br As B4XView= xui.CreatePanel("l")
    br.SetLayoutAnimated(100,0,0,100%x,50dip)
    br.LoadLayout("l_items")
    cur.Position=i
    Label1.Text=cur.GetString("dif")
    Label2.Text=cur.GetString("voc")
    Label3.Text=cur.GetString("meaning")
    Label3.TextColor=Colors.Red
    jo.InitializeContext.RunMethod("zxc",Array As Object(Label1))
    clv_item.Add(br,"")
Next
Anyway I think that you should not use Cursor nowadays but only ResultSet.
Someone more expert in DB will help you further.
I don't even know if my code will work.
actually I put your codes and got this ERROR
android.database.CursorIndexOutOfBoundsException: Index 3 requested, with a size of 3
I dont know what is means
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I dont know what is means
the arraysize is 3 items (index 0, 1 and 2) but you are accessing index 3.
You are trying to get 10 rows from the cursor. But it´s size is only 3.

Use RESULTSET

B4X:
Dim SenderFilter As Object = db.ExecQueryAsync("SQL", "SELECT * FROM tblvoc where id in (1,2,3,4.5)", Array())
Wait For (SenderFilter) SQL_QueryComplete (success As Boolean, rs As ResultSet)
If success Then
    Do While rs.NextRow
         ' get your data here....
      Dim br As B4XView= xui.CreatePanel("l")
      br.SetLayoutAnimated(100,0,0,100%x,50dip)
      br.LoadLayout("l_items")
      Label1.Text=cur.GetString("dif")
      Label2.Text=cur.GetString("voc")
      Label3.Text=cur.GetString("meaning")
      Label3.TextColor=Colors.Red
      jo.InitializeContext.RunMethod("zxc",Array As Object(Label1))
       clv_item.Add(br,"")
    loop
    rs.close
end if
 
Last edited:
Upvote 0

aligol

Member
the arraysize is 3 items (index 0, 1 and 2) but you are accessing index 3.
You are trying to get 10 rows from the cursor. But it´s size is only 3.

Use RESULTSET

B4X:
Dim SenderFilter As Object = db.ExecQueryAsync("SQL", "SELECT * FROM tblvoc where id in (1,2,3,4.5)", Array())
Wait For (SenderFilter) SQL_QueryComplete (success As Boolean, rs As ResultSet)
If success Then
    Do While rs.NextRow
         ' get your data here....
      Dim br As B4XView= xui.CreatePanel("l")
      br.SetLayoutAnimated(100,0,0,100%x,50dip)
      br.LoadLayout("l_items")
      Label1.Text=cur.GetString("dif")
      Label2.Text=cur.GetString("voc")
      Label3.Text=cur.GetString("meaning")
      Label3.TextColor=Colors.Red
      jo.InitializeContext.RunMethod("zxc",Array As Object(Label1))
       clv_item.Add(br,"")
    loop
    rs.close
end if
Thanks dear and excuse me for taking your time but I'm super newbie here I'm trying to figure out. would you please fix these that I attached
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Thanks dear and excuse me for taking your time but I'm super newbie here I'm trying to figure out. would you please fix these that I attached
If possible you should attach your project here to let anyone check it with your DB.
To do this use the EXPORT AS ZIP function from B4A as you are working with Activities (this is another thing that will give you some problems, remember to start new project as B4XPages and not as Default).
 
Upvote 0
Top