Android Question List/Inputlist Vs Listview

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi all,

I am trying to replace the combination of List&Inputlist with Listview but despite reading everything from the the Beginners Guide to the tutorial etc. I can't get Listview to Gel in my head. I can see by the tutorial how to create one and change it's height/width/colour/ add items etc but not how to get data in and out as I require.

What I am doing is storing a history of results in a push down stack and reading past values as required. The code I am using is below, it functions OK but Listview would look better.


Sub Process_Globals
Dim History As List
...
End Sub

History.Initialize
History.InsertAt(0, 0) 'Puts a "start" value on the stack

Sub StackPush
History.InsertAt(0, Value) 'Pushes value on to top of stack
If History.Size > 15 Then
History.RemoveAt(15) ' Removes value at bottom of stack to limit stack size
End If
End Sub

Sub StackRecall
Dim p As Int
p = InputList(History,"History",-1) 'Displays list and allows selection of item
Value = History.get(p) 'Retrieves value of selected item
End Sub


It would be much appreciated if someone can inject some Listview understanding in to my brain.

Regards Roger
 

klaus

Expert
Licensed User
Longtime User
List and ListView are two different things.
List is an object and ListView is a view, there is no link between them.
You need to define the ListView and fill it.

Attached a very simple example.
 

Attachments

  • ListViewSimple.zip
    15.1 KB · Views: 106
Last edited:
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
List and ListView are twi different things.
List is an object and ListView is a view there is no link between them.
You need to define the ListView and fill it.

Attached a very simple example.


Thanks again Klaus,

I must be asking the wrong question.
I understand that there is no link between the two and I have previously looked at the example you sent. Didn't help.
As I said in the original post I can create a Listview but not how to get data in and out as I require.

I will try asking the right question/s:
In the example you sent:
1. How do I store "value1" in the top item?
2. How do I move "value1" to the next item down so I can then store "value2" in the top item?
3. How do I move both values down so I can store "value3" in the top item ... Etc.?
4. How do I retrieve any one of these values?

I hope this makes sense, this is where I am stuck.

Regards Roger
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
1. How do I store "value1" in the top item?
You can store whatever you want in a listview. Use
B4X:
lv.AddSingleLine2
or
B4X:
lv.AddTwoLines2
to give an value when you add this item to the listview.

See this on an principle you can use when you want to store more than a single value into one item.

B4X:
    For i = 0 To 20
        Dim retmap As Map
        retmap.Initialize
        retmap.Put("tag","TagValue"&i)
        retmap.Put("itemID","ItemID"&i)
        retmap.Put("name","ItemName"&i)
        retmap.Put("line1","This is line 1")
        retmap.Put("line2","This is line 2")
        lv.AddTwoLines2(retmap.Get("line1"), retmap.Get("line2"), retmap)
     
    Next

Sub lv_ItemClick (Position As Int, Value As Object)
    Dim retmap As Map
    retmap = Value
    Log("Item at Pos "&Position&" Clicked...")
    Log("ItemInfos:")
    Log("tag = "&retmap.Get("tag"))
    Log("itemID = "&retmap.Get("itemID"))
    Log("name = "&retmap.Get("name"))
    Log("line 1 = "&retmap.Get("line1"))
    Log("line 2 = "&retmap.Get("line2"))
End Sub

2. How do I move "value1" to the next item down so I can then store "value2" in the top item?

You should use a list (not listview) to get the topvalues in memory... if you need a new place 1 you insert an item in this list at the beginning.
Or if you want to chang a value; do it in the list.
After that you clear the listview and build the items again from beginning based on your list.
Same will work in Q3
4. How do I retrieve any one of these values?
Use
B4X:
lv.AddSingleLine2
or
B4X:
lv.AddTwoLines2
to give an value when you add this item to the listview. The Value you give here is the value you get in lv_itemclick event.
See my principle-code above to be inspired
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
DonManfred & Klaus

Thanks for your persistent efforts, after working through DonManfreds examples I went back to Klaus's examples and after re-wiring my brain it finally came together. For completeness and hopefully help to others, is a "trimmed" version of my resulting code.
Firstly I was trying to replace the combination of List&Inputlist with Listview.
It should have been replace List&InputList with List&Listview&Listview_ItemClick.

Sub Process_Globals
Dim History As List 'List where data is stored
Dim Value as Double ' Used to pass values from program to List and from ListView to program
...
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 HistoryView As ListView 'Basically a mechanism for displaying data from a List, Array, Map Etc.
...
End Sub


Sub Activity_Create(FirstTime As Boolean)
History.Initialize
History.InsertAt(0, 0) 'Puts a "start" value on the stack
HistoryView.Initialize("HistoryView")
....
End Sub

Sub StackPush
History.InsertAt(0, Value) 'Pushes value on to top of stack
If History.Size > 15 Then
History.RemoveAt(15) ' Removes value at bottom of stack to limit stack size
End If
End Sub

Sub Stackrecall2
Dim Temp As Double

Dim HistLabel As Label 'Set the parameters for displaying the Items of the ListView
HistLabel = HistoryView.SingleLineLayout.Label '
HistLabel.TextSize = 20 '
HistLabel.TextColor = Colors.White '
HistLabel.Color = Colors.Transparent '
HistLabel.Height = 30dip 'Label height and Item Height should match to avoid gaps & overlaps
HistoryView.SingleLineLayout.ItemHeight = HistoryView.SingleLineLayout.Label.Height
HistoryView.Color = Colors.Black 'ListView color matches Label color as labels don't fill ListView
HistoryView.ScrollingBackgroundColor = Colors.Blue

HistoryView.Clear 'Clear Listview before populating or else you get duplicates
For i = 0 To History.Size - 1 'Builds the viewable list HistoryView from the List "History"
Temp = History.Get(i) 'Get the value from the List
HistoryView.AddSingleLine(Temp) 'Store the value in the ListView
Next
Activity.AddView(HistoryView, 0, 0, 100%x, 100%y) 'Displays the list built by the previous loop.
End Sub


Sub HistoryView_ItemClick (Position As Int, HistValue As Object)
Value = HistValue 'Capture the value retrieved from ListView for use elsewhere.
HistoryView.RemoveView 'Exit ListView.
End Sub

Please let me know if the code shows I have the wrong idea about something
Thanks again Guys

Regard Roger
 
Last edited:
Upvote 0
Top