Android Question CustomListView - Read Value and Index from EditText_Click

Declan

Well-Known Member
Licensed User
Longtime User
This is a simple "Diary" app and I will write any text entered into the EditText to a SQLite database table when the user presses the "Done" button on the keypad.
In order to do this, I must be able to read the "Value"/"Index" of the CustomListView.
I can do so with:
Code:
B4X:
Sub clvEvents_ItemClick(Index As Int, Value As Object)Log(Index & " = " & Value)End Sub
But I must be able to read the Value from the "txtEvents_Click" event.
I tried:
Code:
B4X:
Sub txtEvents_Click(Index As Int, Value As Object)Log("From txtEdit: " & Value)End Sub
But this does not work.
I have attached the app
 

Attachments

  • DiaryExample.zip
    15.6 KB · Views: 222

inakigarm

Well-Known Member
Licensed User
Longtime User
The TextEdit Events are:

B4X:
Sub txtEvent_EnterPressed

End Sub

Sub txtEvent_TextChanged (Old As String, New As String)
 
End Sub

Sub txtEvent_FocusChanged (HasFocus As Boolean)
 
End Sub

Click it's not a TextEdit view Event (is a button view event, etc...)

EDIT: I saw this post comes from another: if you want to detect the index of the selected EditText, put the index value (mycount) on base panel.tag of each CLV cell when you're building the CLV:
B4X:
Sub CreateListItemEvents(Text As String, Width As Int, Height As Int,index As Int) As Panel
    Dim p As Panel
    p.Initialize("")
    'we need to add the panel to a parent to set its dimensions. It will be removed after the layout is loaded.
    Activity.AddView(p, 0, 0, Width, Height)
    p.LoadLayout("CellItemEvents")
   
    'label1 and button1 will point to the last added views.
    lblEvents.Text = Text
    'txtEvent.Tag=index
    p.Tag=index
    p.RemoveView
    Return p
End Sub
and to retrieve this index, get the sender that fires the event and his parent (the base panel of each cell)
B4X:
Dim panel1 as panel
Dim te As EditText = Sender
panel=te.Parent
   
Log("pan=" & panel.tag)
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
You can get the Index of the view with:
B4X:
Private Index As Int = clvEvents.GetItemFromView(Sender)
Example:
B4X:
Sub txtEvent_EnterPressed
   Private edt As EditText = Sender
   Private Index As Int = clvEvents.GetItemFromView(Sender)  
   Log("Text" & Index & " = " & edt.Text)
End Sub
If you use the EnterPressed event be careful, because if the user clicks on another EditText the EnterPressed event will not be raised but the text remains in the EditText.
You should consider using the FocusChanged event and check when the EditText looses focus with HasFocus = False.

You should never use pure pixels:
TabHeight = 30 should be TabHeight = 30dip

You should remove the CustomListViews from the Activity like this:
B4X:
clvEvents.AsView.RemoveView
tbhMain.AddTab2("Events",clvEvents.AsView)
clvClasses.AsView.RemoveView
tbhMain.AddTab2("Classes", clvClasses.AsView)
clvSports.AsView.RemoveView
tbhMain.AddTab2("Sports", clvSports.AsView)
If you don't they remain on the Activity.
When you first run your project and click on an item the Sub clvEvents_ItemClick routine is not called but Sub clvSports_ItemClick is called because clvSports is the View in front.

You can remove these lines, they are not needed.
B4X:
'    IME.Initialize("IME")
'    txtEvent.Initialize("txtEvent")
'    IME.AddHandleActionEvent(txtEvent)
Anyway this doesn't work because txtEvent is not yet known.
You dim one and initialize it, but this one doesn't point to the txtEvent views in the CustomListView.

Attached a modified version of your project.
 

Attachments

  • DiaryExampleNew.zip
    15.5 KB · Views: 221
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
@klaus
Awesome, many thanks - much appreciated.
Using the TabHost, I have two problems that I am tackling.
When the keypad is displayed, the CLV does not scroll up and the keypad covers the EditText that has focus.
The Tabs do not behave as tabs normally do.
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
When the keypad is displayed, the CLV does not scroll up and the keypad covers the EditText that has focus.
Got this sorted by adding the following to the Manifest:
B4X:
SetActivityAttribute(main, android:windowSoftInputMode, adjustPan|stateHidden)
 
Upvote 0
Top