Update ListView at RunOnUiThread

alberto.palberto.p ITMember ✭✭
edited April 2013 in Xamarin.Android

Hi,
I have in a fragment activity a listview that I want to update into RunOnUiThread().
So, the adapter is binding with List<>. The list changes in the content and in the number of elements.

I tryed to update with the following code, but I get Out of memory error:

this.RunOnUiThread(() =>{

    _listAdapter=new SatelliteAdapter(Activity, this.View.Context, Resource.Layout.list_item, listdata.ToArray());
    _ListView.Adapter=_listAdapter;
});

How can I update the ListView?

Posts

  • TeHaTeHa DEMember ✭✭✭
  • alberto.palberto.p ITMember ✭✭

    I tryed, but doesn't work

  • YamilBrachoYamilBracho VEMember
    edited April 2013

    Your are Always creating a adapter for your ListView, so try this :

    this.RunOnUiThread(() =>{
      if (_listAdapter == null) 
      {
         _listAdapter=new SatelliteAdapter(Activity, this.View.Context, Resource.Layout.list_item, listdata.ToArray());
         _ListView.Adapter = _listAdapter;
      }
      else
      {
         _listAdapter.NotifyDataSetChanged();
      }  
    });
    

    `

  • alberto.palberto.p ITMember ✭✭

    @YamilBracho I tried, but doesn't work.
    So, my listview is in fragment into TabViewPager (https://github.com/xamarin/monodroid-samples/tree/master/ViewPagerIndicator).
    The fragment is recreated when you switch from one tab to another, that's why I was recreating the adapter.

  • TomOpgenorthTomOpgenorth CAXamarin Team Xamurai

    Can you post the stack trace for the Out of Memory Error?

  • alberto.palberto.p ITMember ✭✭
    edited May 2013

    @topgennorth

    Clamp target GC heap from 48.792MB to 48.000MB
    Clamp target GC heap from 49.685MB to 48.000MB
    Clamp target GC heap from 49.669MB to 48.000MB
    Forcing collection of SoftReferences for 2896-byte allocation
    Clamp target GC heap from 49.668MB to 48.000MB
    Out of memory on a 2896-byte allocation.Thread started: 
    
    --- decoder->decode returned false
    Clamp target GC heap from 49.691MB to 48.000MB
    Clamp target GC heap from 49.682MB to 48.000MB
    Forcing collection of SoftReferences for 3104-byte allocation
    Clamp target GC heap from 49.682MB to 48.000MB
    Out of memory on a 3104-byte allocation.
    Clamp target GC heap from 49.688MB to 48.000MB
    Clamp target GC heap from 49.689MB to 48.000MB
    Forcing collection of SoftReferences for 3104-byte allocation
    Clamp target GC heap from 49.689MB to 48.000MB
    Out of memory on a 3104-byte allocation.
    Clamp target GC heap from 49.744MB to 48.000MB
    
    *** Uncaught remote exception!  (Exceptions are not yet supported across processes.)
    java.lang.RuntimeException: Out of memory
        at android.os.Binder.execTransact(Binder.java:377)
        at dalvik.system.NativeStart.run(Native Method)
    Caused by: java.lang.OutOfMemoryError
        at java.util.ArrayList.add(ArrayList.java:114)
        at android.location.LocationManager$GpsStatusListenerTransport.onNmeaReceived(LocationManager.java:1413)
        at android.location.IGpsStatusListener$Stub.onTransact(IGpsStatusListener.java:99)
        at android.os.Binder.execTransact(Binder.java:367)
    
  • TomOpgenorthTomOpgenorth CAXamarin Team Xamurai

    Is that the entire stack trace? I was hoping there would be more information. Looking at what you've posted above, all I can really say/guess is that you've probably implemented your own IGpsStatusListener which seems to be leaking memory. Maybe you're maintaining a list of the user's location as they are moving?

  • alberto.palberto.p ITMember ✭✭

    @topgenorth Yes, I have implemented NMealistener and I would like to update lisview of satellite information.
    Maybe i solved. How can I send personally you my code?

  • alberto.palberto.p ITMember ✭✭

    I solved with my custom BaseAdapter

  • bytem2007bytem2007 TNMember

    @Alberto.p: haw are you fixing the problem ?

  • alberto.palberto.p ITMember ✭✭

    @bytem2007‌ I use ViewHolder into adapter.

  • amitsomaiyaamitsomaiya USMember
    edited February 2015

    @Yamil Bracho, Thank you.

    It worked for me. I used as you suggested.

    this.RunOnUiThread(() =>{
    if (_listAdapter == null)
    {
    _listAdapter=new SatelliteAdapter(Activity, this.View.Context, Resource.Layout.list_item, listdata.ToArray());
    _ListView.Adapter = _listAdapter;
    }
    else
    {
    _listAdapter.NotifyDataSetChanged();
    }
    });

Sign In or Register to comment.