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

查看:14
本文介绍了ListViews - 如何在 API 11 之前使用 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);

然后……

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

在 API 11 中引入 addAll() 之前,您是如何进行此操作的?谢谢.

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 这里的来源,实际的实现比你想象的要简单得多.有很多替代方法可以实现这一点,例如:

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 extendsandroid.widget.BaseAdapter,您可以完全控制私有实例变量和方法,可以定义任何你想要的方法自己的实施.互联网上有很多教程告诉如何创建自定义适配器,如此处此处.
  • 选项 2:实现您自己的 ArrayAdapter extendsandroid.widget.ArrayAdapter,然后添加需要的公共方法addAll() 到您自己的 ArrayAdapter 实现,您没有android.widget.ArrayAdapter 中私有成员的可见性所以需要使用现有的公共 API ArrayAdapter.add() 添加每个循环中的元素.

选项 1 是首选并且非常常用,尤其是在您需要在 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.

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

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