Android Question CustomListView 130 ITEMS Downsampling image due to lack of memory

rscheel

Well-Known Member
Licensed User
Start with 10 items, then adding 10 items, when it reaches 130 items the application closes.

Screenshot_20170725-162655.png

Error Log.

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Class not found: anywheresoftware.b4a.samples.customlistview.customlistview, trying: cl.cardview.listview.customlistview
** Activity (main) Resume **
Downsampling image due to lack of memory.
Downsampling image due to lack of memory: 2
Downsampling image due to lack of memory.
Downsampling image due to lack of memory: 2
Downsampling image due to lack of memory.
Downsampling image due to lack of memory: 2
Downsampling image due to lack of memory: 4
Downsampling image due to lack of memory: 8
Downsampling image due to lack of memory.
Downsampling image due to lack of memory: 2
Downsampling image due to lack of memory: 4Downsampling image due to lack of memory: 8Downsampling image due to lack of memory: 16
Downsampling image due to lack of memory: 32
java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available
 

eps

Well-Known Member
Licensed User
You need to use another container... https://www.b4x.com/android/forum/threads/out-of-memory-on-listview.67706/#post-428901

Of course it also depends on the pixel size of the images and how you're defining the bitmap - especially if you are defining it every time... You might want to check this as well... https://www.b4x.com/android/forum/t...-and-loads-more-when-scroll-tot-bottom.35761/

I would recommend you consider Ultimate Scrollview by @Informatix see here https://www.b4x.com/android/forum/threads/lib-chargeable-ultimatelistview.22736/
 

rscheel

Well-Known Member
Licensed User
You need to use another container... https://www.b4x.com/android/forum/threads/out-of-memory-on-listview.67706/#post-428901

Of course it also depends on the pixel size of the images and how you're defining the bitmap - especially if you are defining it every time... You might want to check this as well... https://www.b4x.com/android/forum/t...-and-loads-more-when-scroll-tot-bottom.35761/

I would recommend you consider Ultimate Scrollview by @Informatix see here https://www.b4x.com/android/forum/threads/lib-chargeable-ultimatelistview.22736/
I understand, the image should be a drawer of a small size, and light weight for example an icon, it does not require a large image to load there since the size is small, thank you.
 

eps

Well-Known Member
Licensed User
But there are also other considerations you need to take in to account otherwise you will run out of memory at some point...

ULV for instance only holds the items it displays in memory. The Recycler work there makes it work really well with thousands of images.

There used to be large heap 'true' but I'm not sure that is applicable any more (might have been deprecated) and it only delays running out of memory as opposed to fixing the issue.
 

rscheel

Well-Known Member
Licensed User
But there are also other considerations you need to take in to account otherwise you will run out of memory at some point...

ULV for instance only holds the items it displays in memory. The Recycler work there makes it work really well with thousands of images.

There used to be large heap 'true' but I'm not sure that is applicable any more (might have been deprecated) and it only delays running out of memory as opposed to fixing the issue.
It is quite right, this time I will be inclined to make this application with android studio, there I have more flexibility with recyclerview and endless, thanks again.
 

eps

Well-Known Member
Licensed User
It is quite right, this time I will be inclined to make this application with android studio, there I have more flexibility with recyclerview and endless, thanks again.
There's no need! ULV or CustomListView, used correctly within B4A will allow you to work as you want to. Admittedly ULV is a paid for library - but it's only a mere 25 USD (or possibly a little more now) and works.

ETA : Of course you've shown us no code whatsoever, so it's quite hard to help or give guidance.
 

rscheel

Well-Known Member
Licensed User
There's no need! ULV or CustomListView, used correctly within B4A will allow you to work as you want to. Admittedly ULV is a paid for library - but it's only a mere 25 USD (or possibly a little more now) and works.

ETA : Of course you've shown us no code whatsoever, so it's quite hard to help or give guidance.
It is a personal project which I do not budget, to pay for a bookstore at the moment, has followed closely the work of ULV, is a fantastic job and worth paying for it, but for what I am looking for at the moment I do not Is profitable.

Of course if I do not have problems to place the code, the image is loaded in an ImageView at the moment as I am testing and designing the appearance of how the list will look, in the last test yesterday load 1000 boot items to try and not Closed the application, Also say that the image load only weighs 1.54KB.

B4X:
Sub Process_Globals
    Dim tm As Timer
    Dim n_item As Long
    Dim min_item As Long
    Dim ultimo_item As Long
End Sub

Sub Globals
    Private CustomList As CustomListView 'I believe in the designer LayoutMain
    Private CardView As CardView 'I believe in the designer LayoutCellItem
    Dim STR As AHSwipeToRefreshMulti  'I believe in the designer LayoutMain
    Private PanelListView As Panel 'I believe in the designer LayoutMain
    Dim RV As RippleView
    Dim RVCARD As RippleView
    Private ButtonVerMas As Button 'I believe in the designer LayoutMscard
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("LayoutMain")
    PanelListView.RemoveView
    STR.AddView(PanelListView)
    'STR.SetColorScheme2(Array As Int (Colors.Red, Colors.Blue, Colors.Green, Colors.Cyan, Colors.Magenta, Colors.Yellow))
    STR.SetColorScheme2(Array As Int (Colors.rgb(192,202,51)))
    ProgressDialogShow2("Cargando, espere...", False)
    n_item = 10
    min_item = 1
    ultimo_item = 1
    Dim l As List
    l.Initialize
    l.Add(CustomList.AsView)
    STR.SwipeableChilden = l
    CargaLista
End Sub

Sub CreateListItem(Text As String, Width As Int, Height 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("LayoutCellItem")
    CardView.LoadLayout("LayoutMscard")
    RVCARD.Initialize(CardView, Colors.rgb(207,216,220), 200, True)
    'RV.Initialize(ButtonVerMas, Colors.rgb(94,53,177), 200, True)
    p.RemoveView
    'label1 and button1 will point to the last added views.
    Return p
End Sub

Sub CargaLista
    Sleep(500)
    For i = min_item To n_item
        CustomList.Add(CreateListItem($"Item #${i}"$, CustomList.AsView.Width, 70dip), 74dip, $"Item #${i}"$)
    Next
    ProgressDialogHide
    min_item = n_item
End Sub

Sub CustomList_ReachEnd
    Log("reach end")
    ultimo_item = 2
    'CustomList.AddTextItem("New item!!!", "")
    STR.Refreshing = True
    STR_Refresh
End Sub

Sub STR_Refresh
    Log("Refresh started")
    'CustomList.Clear
    'Start the timer. Normally you will start your asynchronous job here
    tm.Initialize("Timer", 2000)
    tm.Enabled = True

    'Disable the STR object so we can not start a new refresh process
    STR.Enabled = True
End Sub

'The timer tick simulates the end of the refreshing process
Sub Timer_Tick
    Log("Refresh stopped")

    'Stop Timer and refresh the listview data
    tm.Enabled = False
    If ultimo_item == 2 Then
        n_item = n_item + 10
        min_item = min_item + 1
        Log("Item a Agregar: " & n_item)
    Else
        CustomList.Clear
        min_item = 1
    End If
    ultimo_item = 1
    CargaLista

    'Stop the spinning disc and enable the STR object again.
    STR.Refreshing = False
    STR.Enabled = True
End Sub

Sub ButtonVerMas_Click
    Dim index As Int = CustomList.GetItemFromView(Sender)
    ToastMessageShow("Checked items: " & CustomList.GetValue(index), False)
End Sub
And attached the complete project
 

Attachments

rscheel

Well-Known Member
Licensed User
The only image loaded is android.png, right?

Don't set with the designer. This will cause the same image to be loaded multiple times.

Load it once and store it as a process global variable and then set ImageView.Bitmap whenever you create a new item.
If it is the only image, thanks @Erel for your answer, I will prove it.
 

rscheel

Well-Known Member
Licensed User
Well it was as follows, as I detect that for example when I load 1000 records the list moves slowly, as if the phone can not move the list. They may be too much seen at the same time. Such a long list requires a lot of memory.

B4X:
Sub Process_Globals
    Dim Imagen As Bitmap
End Sub

Sub Activity_Create(FirstTime As Boolean)
       Imagen = LoadBitmap(File.DirAssets, "android.png")
       CargaLista
End Sub

Sub CargaLista
    For i = 1 To 10
        CustomList.Add(CreateListItem($"Item #${i}"$, CustomList.AsView.Width, 70dip), 74dip, $"Item #${i}"$)
        ImageView.Bitmap = Imagen
    Next
End Sub
 

eps

Well-Known Member
Licensed User
Hi @rscheel you're almost there! Understood about not wanting to spend 25USD...

Does the list of items shown change at all, whilst viewing it?

I would recommend looking at the following, which should help you. https://www.b4x.com/android/forum/t...ore-when-scroll-tot-bottom.35761/#post-210230

and then lower down in that thread, this https://www.b4x.com/android/forum/t...ore-when-scroll-tot-bottom.35761/#post-233136

Bump/page scrolling.. Ideally you want to hold items in 'pages'.. If you're on the first page you hold the first 20 and display them and get the next 20.. As someone scrolls down you show the next 20 (already retrieved) and grab the next 20, moving the first 20 to the previous 20... and so on... if someone scrolls back up you show them the previous 20 and retrieve the 20 before that... etc.. I'm sure you get the idea.
 

rscheel

Well-Known Member
Licensed User
Hi @rscheel you're almost there! Understood about not wanting to spend 25USD...

Does the list of items shown change at all, whilst viewing it?

I would recommend looking at the following, which should help you. https://www.b4x.com/android/forum/t...ore-when-scroll-tot-bottom.35761/#post-210230

and then lower down in that thread, this https://www.b4x.com/android/forum/t...ore-when-scroll-tot-bottom.35761/#post-233136

Bump/page scrolling.. Ideally you want to hold items in 'pages'.. If you're on the first page you hold the first 20 and display them and get the next 20.. As someone scrolls down you show the next 20 (already retrieved) and grab the next 20, moving the first 20 to the previous 20... and so on... if someone scrolls back up you show them the previous 20 and retrieve the 20 before that... etc.. I'm sure you get the idea.
Well thank you for your answer, but I have already added it to my project from the beginning, first charge 10 elements, then when doing scroll another 10 and so on, if you look at the attached project you will be able to realize it.
 

eps

Well-Known Member
Licensed User
Well thank you for your answer, but I have already added it to my project from the beginning, first charge 10 elements, then when doing scroll another 10 and so on, if you look at the attached project you will be able to realize it.
Sorry - I thought you were saying it was slow - or are you still saying that?

I'll take a look later on or tomorrow.
 

rscheel

Well-Known Member
Licensed User
Sorry - I thought you were saying it was slow - or are you still saying that?

I'll take a look later on or tomorrow.
What I wanted to say is that when accumulating too many items, for example 1000, the scroll becomes in the middle, so it would be nice to only show the elements of 2 pages as maximum and those that are outside those pages to remove and incorporate when you return To your page. As does recyclerview, recycle items that are not in the view....
 

eps

Well-Known Member
Licensed User
What I wanted to say is that when accumulating too many items, for example 1000, the scroll becomes in the middle, so it would be nice to only show the elements of 2 pages as maximum and those that are outside those pages to remove and incorporate when you return To your page. As does recyclerview, recycle items that are not in the view....
Sorry - have you still got an issue in the code in post 8 or not?
 
Top