MonoDroid的 - 根据GREF极限传递数据的ListView [英] Monodroid - passing data to ListView according to GREF limit

查看:112
本文介绍了MonoDroid的 - 根据GREF极限传递数据的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#,单声道为Android。我需要输出到ListView中的很大一部分合并数据。要做到这一点,我用适配器以下明显的方式:

C#, Mono for Android. I need to output a large portion of combined data into ListView. To achieve this, I use the following obvious approach with Adapter:

class ItemInfo
{
    public string Id;
    public string Name;
    public string Description;
    public int Distance;

    //Some more data
}


class ItemAdapter : ArrayAdapter<ItemInfo>
{
    public WorldItemAdapter (Context context, int textViewResourceId, List<WorldItemInfo> items) :
    base(context, textViewResourceId, items)
    {   
    }

    //...

    public override View GetView (int position, View convertView, ViewGroup parent)
    {
        //Some stuff to format ListViewItem
    }
}


public class OutputActivity : Activity
{
    ListView _listView;

    //...

    void FillList (object SomeParameters)
    {
        var adaptedList = someDataSource.Where().Join().Union().//anything can be imagined
        .Select ((item, item2, item3) => 
            new ItemInfo (){                
                Id = item.Id,
                Name = item.Name,
                Description = String.Format(..., item2, item3),                 
                Distance = ...,
                //so on             
            }
            ).OrderBy ((arg) => arg.Name).ToList ();

        _listView.Adapter = new ItemAdapter (this, Resource.Layout.ListItemFormat, adaptedList  ());

    }
}

这工作得很好...直到我开始频繁地刷新我的ListView。如果我产生许多ItemInfo的(通过刷新我的看法,例如),我到GREF限制很快(形容这里,意外NullReferenceException异常一节),我的应用程序崩溃。展望Android的日志中,我可以看到成千上万Android.Runtime.IJavaObject对象,溢出GREF限制。

This works very fine... until I start to refresh my ListView frequently. If I generate many ItemInfo's (by refreshing my view, for example), I reach GREF limit soon (described here, "Unexpected NullReferenceExceptions" section), and my application crashes. Looking into Android log, I can see thousands of Android.Runtime.IJavaObject objects, which overflow GREF limit.

单VM + Dalvik虚拟机桥我能理解,概念,我的ItemInfo的需要要传递给Dalvik虚拟机,包裹IJavaObject和由天然环境中的ListView被格式化 - 这产生GREF的。只要垃圾回收是一个非确定的过程中,如果我叫FillList()很多次了,老了,已经使用ItemInfo的逗留到内存,漏水。

According to concepts of Mono VM + Dalvik VM bridge I can understand, that my ItemInfo's need to be passed to Dalvik VM, wrapped to IJavaObject and to be formatted in ListView by native environment - this creates GREF's. As long as garbage collecting is a non-determined process, if I call FillList() many times, old, already used ItemInfo's stay into memory, leaking.

我怎样才能避免泄露?或者,可能是有另一种方式格式化的数据到ListView控件输出很大一部分?我想:

How can I avoid leaking? Or, possible, is there another way to output large portions of formatted data into ListView? I tried:


  • 我不能减少ItemInfo的数量,只要我需要以某种方式把我的数据。

  • 我可以不遵循这个<一个href=\"http://stackoverflow.com/questions/7443818/monodroid-gref-problem-best-practice/7444723#7444723\">advice,只要我的ItemInfo不是IJavaObject。

  • 作为一个暂时的解决方案,我调用GC.Collect()每次我需要刷新列表,但是这看起来不是一个干净的方式时间。另外,如果我需要输出超过2к物体进入名单,这并不能帮助。

  • I can't reduce the number of ItemInfo's, as long as I need to place my data somehow.
  • I can't follow this advice, as long as my ItemInfo is not an IJavaObject.
  • As a temporarily solution, I call GC.Collect() every time I need to refresh list, but this looks not a clean way. Also, if I need to output more than 2к objects into list, this doesn't help.

推荐答案

答找到。我继承了我ItemInfo类从java.lang.Object中明确,现在可以调用Dispose()时不再需要的对象。这杀死泄漏GREFs。

Answer found. I've inherited my ItemInfo class from Java.Lang.Object explicitly and now can call Dispose() when object is no longer needed. This kills leaking GREFs.

这篇关于MonoDroid的 - 根据GREF极限传递数据的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆