列表视图 - 11 API之前,如何使用ArrayAdapter.addAll()函数? [英] ListViews - how to use ArrayAdapter.addAll() function before API 11?

查看:778
本文介绍了列表视图 - 11 API之前,如何使用ArrayAdapter.addAll()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新一个全新的ArrayList一个ListView。对于API 11它的伟大工程使用的addAll(...)方法,但这并不适用于早期的API。我无法弄清楚如何去更新整个列表像这样的旧版本。

I'm trying update a ListView with an entirely new ArrayList. For API 11 it works great using the addAll(...) method, but this is not available for earlier APIs. I can't figure out how to go about updating an entire list like this for older versions.

ArrayAdapter<String> textList = new ArrayAdapter<String>(listener, R.layout.list_item, stringList);
listener.setListAdapter(textList);

再后来......

Then later...

textList.clear();
textList.addAll(stringList); <--- works fine for Android 3.0 (API Level 11) and beyond, but not before. 

你是怎么去的addAll在此之前()的API 11被引入?谢谢你。

How did you go about doing this before addAll() was introduced in API 11? Thanks.

推荐答案

最简单的方法是避免使用ArrayAdapter.addAll()和ArrayAdapter.add()在一个循环中,在他的回答像idiottiger建议。

The simplest way is avoid using ArrayAdapter.addAll() and ArrayAdapter.add() within a loop, like idiottiger suggested in his answer.

如果您坚持使用ArrayAdapter.addAll(),简短的回答是DIY。退房android.widget.ArrayAdapter源<一href="https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/ArrayAdapter.java">here,实际执行是比你想象的要简单得多。有许多替代方案来实现这一点,例如:

If you insist to use ArrayAdapter.addAll(), The short answer is DIY. Check out the source of android.widget.ArrayAdapter here, the actual implementation is much simpler than you thought. there are many alternatives to achieve this, for instance:

  • 选项1:实现你自己ArrayAdapter延伸 android.widget.BaseAdapter,你会得到完全控制私有实例 变量和方法可以定义任何方法你想要在你的 自己的实现。有很多教程在网上讲述了 创建自定义适配器,喜欢这里 和的here
  • 选项2:实现你自己ArrayAdapter延伸 android.widget.ArrayAdapter,然后添加所需的公共方法 的addAll(),以自己的ArrayAdapter实现,你没有 在android.widget.ArrayAdapter知名度的私有成员因此需要 利用现有的公共API ArrayAdapter.add()添加每一个 一个循环中的元素。
  • Option 1: Implement you own ArrayAdapter extends android.widget.BaseAdapter, you get full control of private instance variable and method and can define whatever method your want in your own implementation. there are many tutorial on the internet tells how to create custom adapter, like here and here.
  • Option 2: Implement you own ArrayAdapter extends android.widget.ArrayAdapter, then add the required public method addAll() to your own ArrayAdapter implementation, you don't have the visibility on private member in android.widget.ArrayAdapter so need use existing public API ArrayAdapter.add() to add every single element within a loop.

选项1是preferred和使用非常普遍,尤其是当你需要你的ListView中呈现更复杂的自定义UI的东西的情况。

Option 1 is preferred and used very commonly, especially in the situation when you need render more complex custom UI stuff within your ListView.

这篇关于列表视图 - 11 API之前,如何使用ArrayAdapter.addAll()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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