Android Tutorial ListView tutorial

eps

Expert
Licensed User
Longtime User
I wonder how I'll be able to allow a user to delete an item from a listview upon long clicking and displaying a menu to them... any help?

You can't really, but.... you _could_ have two ListViews and flip between them as you remove items.
 

Kwame Twum

Active Member
Licensed User
Longtime User
thanks eps, but actually my listview populates from a file created on the SDcard through a list.

B4X:
Dim list1 = File.ReadList(File.DirRootExternal, "folder1/listfile.txt")
For  i = 0 To list1.Size
i=i+1  'ignoring first item in text file
lview.AddSingleLine(translist.Get(i))  'lview is the listview added to Globals from the designer
Next

Is there anyway I could delete just a line from the text file on the SDcard?

The plan is to display a menu upon long clicking an item in the listview, then deleting the line from the text file when the user clicks "delete" from the menu. Help.
 

Kwame Twum

Active Member
Licensed User
Longtime User
I thought of that... but how do you determine the index if the item that was long clicked?
Searched, but there was nothing like "SelectedItem" or any of its kind..
 

NJDude

Expert
Licensed User
Longtime User
I would suggest you read the docs or read the beginner's guides (click on the DOCUMENTATION link on my signature) all that is explained there and there are also samples, it will make your life easier.
 

Kwame Twum

Active Member
Licensed User
Longtime User
Solved. created a type with two parameters
B4X:
'My Class - class module
Sub Class_Globals
    Type MyListViewData (FirstRow As String, SecondRow As String)
End Sub

Activity Module

B4X:
Sub Globals
Dim mylistz as listview
End Sub

Dim newlist As List:newlist.Initialize
'Get first and second items in each listview item and save it as a list

For i=0 To mylistz.Size-1   'mylistz wasn't initialised cuz it's generated by designer
      Dim mydata As MyListViewData
      mydata=mylistz.GetItem(i)
      newlist .Add(mydata.FirstRow)  'add first row of current item in listview
      newlist .Add(mydata.SecondRow) 'add second row of current item listview
Next

Newlist is populated with all lines
 

StarinschiAndrei

Active Member
Licensed User
Longtime User
Hello,
How can i read the tag value in listview ?

Sub LoadList(itemName As String )
lv1.Clear
lv1.Initialize("ListView1")
lblarticle=lv1.SingleLineLayout.Label
lv1.SingleLineLayout.ItemHeight = 40dip
lv1.SingleLineLayout.Label.TextSize = 14
lv1.SingleLineLayout.Label.TextColor = Colors.Black
lv1.SingleLineLayout.Label.Gravity = Gravity.LEFT
lv1.ScrollingBackgroundColor=Colors.Transparent
lv1.Color=Colors.White
For i = 1 To 300
lv1.AddSingleLine(itemName & i)
lv1.SingleLineLayout.Label.Tag = i
Next
Activity.AddView(lv1,0 ,35%x, 100%x,40%y)
End Sub
 

StarinschiAndrei

Active Member
Licensed User
Longtime User
You cannot set nor get the Tag property of a given Label in a ListView.
All properties for lv1.SingleLineLayout.Label are valid for all Labels.
What do want to do with the Tag ?

Best regards.
Hi Klaus ,
i want to store a value in tag for each items in the listview. Than i click on one item and read tag value.
Thank you
 

klaus

Expert
Licensed User
Longtime User
i want to store a value in tag for each items in the listview. Than i click on one item and read tag value.
When you click on an item in a ListView the ItemClick (Position As Int, Value As Object) event will be raised.
And Position is the index of the selected item, so no need for the Tag property.

Best regards.
 

StarinschiAndrei

Active Member
Licensed User
Longtime User
When you click on an item in a ListView the ItemClick (Position As Int, Value As Object) event will be raised.
And Position is the index of the selected item, so no need for the Tag property.

Best regards.
Hi Klaus ,

I know , but i wanted to assign a diffrent value than position or value. this is the reason that i thought that i can use the tag.
 

GMan

Well-Known Member
Licensed User
Longtime User
Hoi,
i try th get the Text of the Labels of the TwoLineAndBitmap items.

This
B4X:
Dim MyItemText as String
Sub ListView1_ItemClick (Position As Int, Value As Object)
    MyItemText = Listview1.TwoLinesAndBitmap.Label.Text
End Sub
returns nothing - but why ?
 

Mahares

Expert
Licensed User
Longtime User
I think you want to do this:
B4X:
For i= 0 To 4
          ListView1.AddTwoLinesAndBitmap2("first" & i,i*i,LoadBitmap(File.DirAssets,"warning-icon.png"),"first" & i)
Next

Sub ListView1_ItemClick (Position As Int, Value As Object)
    Dim MyItemText As String =Value
    Msgbox("I chose: " & MyItemText,"Selected Item")
End Sub
 

GMan

Well-Known Member
Licensed User
Longtime User
Thx so far - i had to decribed it better:
i want to transfer the seletced label text into another activity.
When DIMming it as a global variable it wont work, other the result is not the text of the label but the value ot the selected index

And when trying
B4X:
Dim MyItemText As String = Listview1.TwoLinesAndBitmap.Label.Text
the ersult is again NOTHING or the the selected INDEX (as Integer)
 

GMan

Well-Known Member
Licensed User
Longtime User
I fill it manually like
B4X:
Listview1.AddTwoLinesAndBitmap2("Label1text","Label2text",LoadBitmap(File.DirAssets ,"someimage.gif"),1)
etc.

In my "results" so far only the return is displayed (1 in this case), but not" Label1text"

Problem is (as written) that after the click a new activity with panel was loaded and the label on the panel should contain "Label1text"
 

GMan

Well-Known Member
Licensed User
Longtime User
SOLVED - i named the return value as the same as the label1.text - so it works

Thx to all 4 helping me
 

luke2012

Well-Known Member
Licensed User
Longtime User

Hi @Erel.
I'm using the "AddTwoLinesAndBitmap" method and I put a test in the first line and also in second line.
To access the text in first line i use : dim item as string=myListView.GetItem(position)
How to access the second line of the item ?
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…