Android Question Copy Listview (AddTwoLinesAndBitmap2) item to another Listview

scottie

Member
Licensed User
Longtime User
I have 2 listviews LV1 & LV2
' LV1.AddTwoLinesAndBitmap2(Myname ,"",icon.Bitmap,TitleName) 'this works
now
I just need to add that LV1 entire item to a new LV2 entry: LV2.AddTwoLinesAndBitmap2....

Any help would be greatly appreciated,
-Scott
 

scottie

Member
Licensed User
Longtime User
Don't waste your time with ListView. Use xCustomListView instead.
That's true. :)
All of this is because you cant do any sorting on a listview / xcustomlistview. Wish I could sort LV1 using Line 1
Funny. First thing you want to do to a big list is sort it. But you can't. Have to send LV1 to a list, then sort, then send back. ha
For singleline lists that's no problem.

So, I'm adding all LV1 to a list, sort it, and picking out what items(X) that I want to add from LV1.getitems(X) to LV2
Problem is LV1 is 2line and bitmap. not a single line.
I need to send LV1.getitem(X) to LV2.AddTwoLinesAndBitmap2 and it contain the bitmap/2 Lines

-Scott
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
How do you fill LV1 ?
If you do not use LV1.AddTwoLinesAndBitmap2 with a Return value it will not work.
If there is no Return value GetItem returns the text of the first line.
You could define a Type variable holding the different data, use AddTwoLinesAndBitmap2 and get the Type value in return.
But without more detailed information it is difficult to give a concrete advice.
A small test project showing the problem would be helpful for the helpers.
 
Upvote 0

scottie

Member
Licensed User
Longtime User
Guess I could add all to 3 different SINGLE listviews using each line and bitmap as the return for each listview.

then add entire listview item using line1 (from 1st listview) to a list. sort that list. at this point names are sorted.
then do a string compare to find the listview item#. then all all the listviews to the final sorted listview

seems like a lot of work and processing just to sort a listview / pick and choose what items i want in new sorted list.
 
Upvote 0

scottie

Member
Licensed User
Longtime User
What exactly do you want to do.
You ask questions but i do not understand your problem.
Expose exactly your problem what do you want to do ?
Then it would be much easier to give concrete advice.
Sort a listview that contains icon.bitmaps by string(line1) or string(line2).
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
If you are trying to run a sort directly on a listview then I think that you are using the wrong approach. In this situation I have my data items in a List; I sort the list as required, then remove, replace and repopulate the listview.
 
Upvote 0

scottie

Member
Licensed User
Longtime User
I had to quickly get this working by morning. I came up with this. Seems to work, with little testing. :)


Sort3Lists2SortedListView:
Sub Sort3Lists2SortedListView
    'create 'SortedList' listview
    'create 4 lists:
    '   list1 is the name which will be sorted
    '   list2 is original name not sorted
    '   list3 is package name
    '   list4 is icon.bitmap
    'init all 4 lists
    'add your data to the 3 lists
    ' example add all installed apps to the lists
    '   for each app installed
    '     List1.Add(name) 'name of app
    '     List3.Add(packName)
    '     List4.Add(icon.Bitmap)
    
    List2=List1 'list2 is to keep original list sequence. Along with List3, & List4
    List1.Sort(True)  'sort list1 but leave other lists alone
    
    Dim SearchFor As String
    Dim N As Int
    
    For N = 0 To List1.Size -1 'pick a line to search for
        SearchFor=List1.Get(N)
    
        Dim MyLine As String
        For II = 0 To List2.Size -1  'search for line1 in list 2 to get list item #
            MyLine = List2.Get(II)
            If MyLine.Contains(SearchFor) Then
                SortedList.AddTwoLinesAndBitmap2(List2.Get(II),List3.Get(II),list4.Get(II),List3.Get(II))
            End If
        Next
        
    Next
    Lv3 = SortedList 'sortedlist is never visible, but LV3 (listview) is
End Sub
 
Upvote 0

emexes

Expert
Licensed User
If you are trying to run a sort directly on a listview then I think that you are using the wrong approach. In this situation I have my data items in a List; I sort the list as required,
+1

but with the added clue that the List should be a single List of a User Defined Type having three fields (eg: name, packName As String, icon As Bitmap?) rather than three separate Lists.

then sort using https://www.b4x.com/android/help/collections.html#list_sorttype

then remove, replace and repopulate the listview.

where "remove, replace" is possibly effectively same as, and more quickly done by, ListView.Clear
 
Upvote 0

emexes

Expert
Licensed User
Maybe later, but for now list2=list1 is working?

If ListView names (line 1) are still with their associated packNames (line 2) and icons, then yes (?)

But I have a feeling that the connection might have been lost.

Unless they were already in name order and the .Sort didn't move anything about.

I'll check in a half-hour when I'm back in front of B4A and an Android device to test it on.
 
Upvote 0

emexes

Expert
Licensed User
I'll check in a half-hour when I'm back in front of B4A and an Android device to test it on.

What I get is that List2 = List1 means that List1 and List2 are then both the same (shared, one, single) List.

And List1.Sort(True) is sorting that one (shared) List.

Thus magically sorting List2 also, ie rearranging the items in List2 but without making the corresponding rearrangements of List3 and List4.

Before and after sorting List1 (and thus also List2 which is used to populate the ListView alongside List3 and List4)

Screenshot_20221228-162547.png
Screenshot_20221228-162609.png



Also if List2 is the original and List1 is for sorting, then shouldn't this:
B4X:
List2=List1 'list2 is to keep original list sequence. Along with List3, & List4
be the other way around ie TemporaryList = OriginalList rather than OriginalList = TemporaryList ?
 
Last edited:
Upvote 0

scottie

Member
Licensed User
Longtime User
OMG! Your right.
I changed it to:

'For x =0 To List1.Size-1
' List2.Add(List1.Get(x))
'Next
then I sort list1

That's so wierd how that works. Kinda sucks. ha ha

Thank you sooooooo much. Now I'm ready for my 9am conference. :)
 
Upvote 0

emexes

Expert
Licensed User
Now I'm ready for my 9am conference. :)

Arghh, I didn't realise there was a deadline involved.

I was sitting here feeling that, rather than dredging up problems, it'd be nicer if I dredged up solutions. šŸ»

So, consider this (if you have time):
(highlighted lines are mildly-unorthodox ways of doing things that might trigger new ideas)

B4X:
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
 
    Type AppType(AppName, PackageName As String, Icon As Bitmap, Price As Double)

    Dim AppList As List

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
 
    Dim Button1, Button2, Button3 As Button
    Private LV3 As ListView
 
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")

    CreateAppList
    AppListToListView

    DeslectAllSortButtons
 
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    '''xui.MsgboxAsync("Hello world!", "B4X")
 
    AppList.SortType("AppName", True)
    AppListToListView
 
    DeselectAllSortButtons
    Button1.Color = Colors.Green
 
End Sub

Sub Button2_Click

    AppList.SortType("PackageName", True)
    AppListToListView
 
    DeselectAllSortButtons
    Button2.Color = Colors.Green
 
End Sub

Sub Button3_Click

    AppList.SortType("Price", True)
    AppListToListView
 
    DeselectAllSortButtons
    Button3.Color = Colors.Green
 
End Sub


Sub DeselectAllSortButtons

    Button1.Color = Colors.Gray
    Button2.Color = Colors.Gray
    Button3.Color = Colors.Gray
 
End Sub

Sub CreateAppList
 
    AppList.Initialize
     
    For I = 1 To 6
        Dim TempApp As AppType
        TempApp.AppName = "Name " & Array As String("One", "Two", "Three", "Four", "Five", "Six")(I - 1)
        TempApp.PackageName = "Package " & I
        TempApp.Icon = LoadBitmap(File.DirAssets, I & ".png")
        TempApp.Price = Array As Double(3.14, 1.59, 2.65, 3.58, 9.79, 3.23)(I - 1)
        AppList.Add(TempApp)
    Next
 
End Sub

Sub AppListToListView

    Dim WhichCurrency As Int = 1
    Dim CurrencySymbol As String = "$ā‚¬Ā„".CharAt(WhichCurrency)
    Dim ConversionRate As Double = Array As Double(1, 0.9392, 133.8885)(WhichCurrency)
 
    LV3.Clear
    For I = 0 To AppList.Size - 1
        Dim TempApp As AppType = AppList.Get(I)
        Dim PrettyPrice As String =  CurrencySymbol & NumberFormat2(TempApp.Price * ConversionRate, 1, 2, 2, False)
        LV3.AddTwoLinesAndBitmap(TempApp.AppName & " " & PrettyPrice, TempApp.PackageName, TempApp.Icon)
    Next
 
End Sub


The ListView when app first started:

Screenshot_20221228-172736.png



The ListView after pressing each sort button:

Screenshot_20221228-172741.png
Screenshot_20221228-172745.png
Screenshot_20221228-172749.png
 
Last edited:
Upvote 0

scottie

Member
Licensed User
Longtime User
This is why your an 'Expert' :)
I will save your code, probably for a better version of my program.
Thanks a bunch
 
Upvote 0

emexes

Expert
Licensed User
This is why your an 'Expert'

Not sure if that's good or bad, given that an ex is a has-been and a spurt is a drip under pressure šŸ¤£

tbh I was mostly just building upon suggestions of my fellow drips:

You could define a Type variable holding the different data
In this situation I have my data items in a List; I sort the list as required, then remove, replace and repopulate the listview.
 
Upvote 0
Top