Spanish boton en customlistview

Miguel Santillan

Member
Licensed User
Buenas noches, los molesto porque he armado un customlistview que contiene un campo label, una imagen y un boton. Lo que pretendo hacer y no puedo es lo siguiente. Al presionar el boton ejecuto algo y no puedo capturar (en realidad no lo se) el momento que presiono en boton en cada ltem del customlistview y taer el valor de uno de los label, o cualquier campo.
He llegado a capturar cual es el index del listview, pero no el valor de cada campo.
Si alguien me pudiera dar una mano con esto, muy agradecido.

Miguel
 

josejad

Expert
Licensed User
Longtime User
Hola Miguel, ninguna molestia.

Lo que comentas se soluciona capturando el “sender”
Method_636.png
Sender As Object

Returns the object that raised the event.
Only valid while inside the event sub.
Example:
B4X:
Sub Button_Click
Dim b As Button
b = Sender
b.Text = "I've been clicked"
End Sub

echa un ojo al tutorial (edito ya que había añadido una imagen en vez del enlace al tutorial)

https://www.b4x.com/etp.html?vimeography_gallery=1&vimeography_video=254862941
 
Last edited:

Miguel Santillan

Member
Licensed User
Hola José, nuevamente te molesto debido a que he visto el tutorial y realmente em siento muy mal porque me está venciendo el tema, y eso es muy humillante.
Lo he probado de todas maneras. Es muy probable que no me haya expresado bien con respecto a la necesidad.
en cada item del customlistvie tengo una imagen, un label y un boton que si se presiona llama a otro activity con el valor del label.
O sea, lo que quiero es saber el numero de item que lo obtengo con "Sender" y el contenido del label que tiene el item elegido.
En un listview cualquiera con el value obtengo el contenido de la linea, quise usar customlistview porque crei que podía sacar cualquier valor, no el de toda la linea.
Bueno, la estoy haciendo muy larga y aburrida, en definitiva no entiendo como sacar el valor de un objeto (label, imagview, edittex, etc) en cada item.
Si se puede hacer algo bienvenido, de lo contrario tambien, muy agradecido y contento con haber obtenido b4a. Hay mucho para aprender.

Saludos y a disposición
Miguel
 

josejad

Expert
Licensed User
Longtime User
Si puedes poner tu código (por favor, entre [code ] ... [ /code] o bien seleccionando en el menú "Code" quizás lo podamos ver más fácil.
1585160767995.png


O bien exporta tu proyecto a ZIP ("Archivo->Exportar como zip") y lo subes. Acostúmbrate cuando preguntes a hacer alguna de estas cosas (poner tu código al menos) para que sea más fácil ayudarte y no andemos dando palos de ciego.

Si entiendo bien, lo que se me ocurre más rápido sería que a la propiedad Tag de tu botón, le asignaras el nombre de la actividad que quieras llamar.
B4X:
button.tag = label.text

Sub Button_Click
Dim b As Button
b = Sender
b.Text = "I've been clicked"
Log("Este es el valor que buscas?: " & b.tag)
End Sub
Fíjate bien también que un ListView en el evento click, no solo devuelve el índice pulsado, sino también un valor, ahí puedes almacenar lo que quieras:
B4X:
Sub clvButtons_ItemClick (Index As Int, Value As Object)
    Log("clvButtons: " & Index & " Valor: " & Value)
End Sub
 

Miguel Santillan

Member
Licensed User
B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
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 CLV1 As CustomListView
    Private Lbl_descripcion As Label
    Private CheckBox1 As CheckBox
    Private BtnClear As Button
    Private ImageView1 As ImageView
    
    Dim Registro As Cursor
    Dim SQL1 As SQL
    Dim L_denominacion As String
    Dim L_id_articulo As String
    Dim L_Unitario As Double
    Dim L_Imagen As String
    Private Lbl_Articulo 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("Custom_articulos")
    Activity.Title = "Lista de Productos"
    Dim images As List
    images.Initialize
    
    'Log(File.DirAssets & " - " & File.DirInternal)
    If File.Exists(File.DirRootExternal,"pedidos_tc.db") = False Then
        ' Ponemos una base inicial de datos en la carpeta Files.
        ' Luego la copiamos a la SD Card
        File.Copy(File.DirInternal,"pedidos_tc.db",File.DirRootExternal,"pedidos_tc.db")
    End If
    
    If SQL1.IsInitialized = False Then
        SQL1.Initialize(File.DirRootExternal, "pedidos_tc.db", False)
    End If

    'Log("Carga la tabla usuarios de la basedatos.sql")
    Registro = SQL1.ExecQuery("SELECT id_articulo, denominacion, unitario, imagen from articulo limit 200")
    For n = 0 To Registro.RowCount - 1
        Registro.Position = n
        L_denominacion   = Registro.GetString("denominacion")
        L_id_articulo    = Registro.GetString("id_articulo")
        L_Unitario       = Registro.GetDouble("unitario")
        L_Imagen         = Registro.GetString("imagen")
        CLV1.Add(CreateItem(L_denominacion.Trim, L_Unitario, L_id_articulo, L_Imagen, CLV1.AsView.Width, 50dip), n)
    Next
        
End Sub

Private Sub CreateItem(Text As String, Importe As Double, Articulo As String, Imagen As String, width As Int,height As Int) As B4XView
    Dim p As B4XView=xui.CreatePanel("")
    p.SetLayoutAnimated(0,0,0,100%x,20%x)
    p.LoadLayout("ItemsLayout")
    p.Color=Colors.Blue
    
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Text = Text
    Dim lbl1 As Label
    lbl1.Initialize("")
    lbl1.Text = Importe
    lbl1.TextColor = Colors.Yellow
    lbl1.TextSize = 20
    Dim lbl2 As Label
    lbl2.Initialize("")
    lbl2.Text = Articulo
    lbl2.Visible = False
    
    p.AddView(lbl,75dip, 2dip, 200dip, height - 4dip)
    p.AddView(lbl1,175dip, 44dip, 200dip, height - 4dip)
    p.AddView(lbl2,175dip, 44dip, 200dip, height - 4dip)
    Return p
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub BtnClear_Click
    Dim b As Button
    b = Sender
    b.Text = " hago click"
    Log(b.Tag)
 

Attachments

  • custom_articulos.jpg
    custom_articulos.jpg
    151.3 KB · Views: 199

josejad

Expert
Licensed User
Longtime User
Hola Miguel:

Aquí estás pasando como "Value" a CLV1.Add el valor n, no sé si lo necesitas. Pero por ejemplo con "Registro.Get(n)" (o lo que aplique) podrías sacar el valor que quieres.
B4X:
CLV1.Add(CreateItem(L_denominacion.Trim, L_Unitario, L_id_articulo, L_Imagen, CLV1.AsView.Width, 50dip), n)

Pero si por ejemplo, le pasas el valor
B4X:
CLV1.Add(CreateItem(L_denominacion.Trim, L_Unitario, L_id_articulo, L_Imagen, CLV1.AsView.Width, 50dip), L_denominacion.Trim) '<---- cambia n por L_denominacion.Trim

En el evento click recuperas el valor, no te haría falta el botón.
B4X:
Sub CLV1_Click (index as int, value as object)
   Log("valor recuperado: " & value)   '<---- Aquí recuperas el valor (segundo parámetro) que pasas en CLV1_Add
End Sub

Si aún así lo quisieras hacer con el botón, pues como te indiqué en el post anterior:
B4X:
Private Sub CreateItem(Text As String, Importe As Double, Articulo As String, Imagen As String, width As Int,height As Int) As B4XView
    Dim p As B4XView=xui.CreatePanel("")
    p.SetLayoutAnimated(0,0,0,100%x,20%x)
    p.LoadLayout("ItemsLayout")
    p.Color=Colors.Blue
   
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Text = Text
    Dim lbl1 As Label
    lbl1.Initialize("")
    lbl1.Text = Importe
    lbl1.TextColor = Colors.Yellow
    lbl1.TextSize = 20
    Dim lbl2 As Label
    lbl2.Initialize("")
    lbl2.Text = Articulo
    lbl2.Visible = False

    btnClear.Tag = lbl1.Text '(o el valor que quieras)  '<-------------------- línea añadida

    p.AddView(lbl,75dip, 2dip, 200dip, height - 4dip)
    p.AddView(lbl1,175dip, 44dip, 200dip, height - 4dip)
    p.AddView(lbl2,175dip, 44dip, 200dip, height - 4dip)
    Return p
End Sub


Sub Button_Click
  Dim b As Button
  b = Sender
  Log("Este es el valor que buscas?: " & b.tag)
End Sub
 
Last edited:
Top