Android 从 ArrayAdapter 获取所有项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11650121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:22:05  来源:igfitidea点击:

Getting all of the items from an ArrayAdapter

android

提问by Edward Dale

I have a ListFragmentbacked by an ArrayAdapterthat gets populated by a Loader. When the user clicks on one of the items, I want to pass a reference to the selected item, as well as the rest of the list items to another fragment. My question is how should I get all of the items from the adapter? Here are the possibilities that I see:

我有一个ListFragment由支持ArrayAdapter是获得由填充Loader。当用户单击其中一个项目时,我想将对所选项目的引用以及列表项目的其余部分传递给另一个片段。我的问题是我应该如何从适配器中获取所有项目?以下是我看到的可能性:

1. Keep a reference to the backing List

1. 保留对支持的引用 List

Create the adapter like so:

像这样创建适配器:

List<DomainObject> items = new ArrayList<DomainObject>();
listAdapter = new ArrayAdapter<DomainObject>(getActivity(), R.layout.mine, items);

and then simply pass itemsor a copy of it to the next activity.

然后简单地将其items或其副本传递给下一个活动。

The downside I see of this is that I'm relying on the undocumented fact that the same list that I pass to the constructor contains the items later on.

我看到的缺点是我依赖于未记录的事实,即我传递给构造函数的同一个列表稍后包含项目。

2. Iterate through the adapter

2. 遍历适配器

When an item is clicked, iterate through the adapter and build up the list. This seems like an unnecessary amount of work. The items are contained in a Listin the adapter and I'm manually copying each item to a new list.

当一个项目被点击时,遍历适配器并建立列表。这似乎是不必要的工作量。这些项目包含在List适配器中的 a中,我正在手动将每个项目复制到一个新列表中。

3. Keep a separate list of items when adding to adapter

3. 添加到适配器时保留一个单独的项目列表

Before adding an item to the adapter, add it to a separate list that I maintain in the fragment. This is also wasteful as the list of items is copied in the ArrayAdapterand the fragment.

在将项添加到适配器之前,将其添加到我在片段中维护的单独列表中。这也是浪费,因为项目列表被复制ArrayAdapter到片段中。

采纳答案by Edward Dale

The solution that I've gone with in the meantime is just to not use ArrayAdapter. In cases where you're fighting against this API, it seems like it's better just to use the less fully-featured (and complex) BaseAdapter. You can read more about the decision to go with BaseAdapterinstead of ArrayAdapterin this article: Android Adapter Good Practices.

我在此期间采用的解决方案是不使用ArrayAdapter. 如果您与此 API 作斗争,似乎最好只使用功能不那么完整(和复杂)的BaseAdapter. 你可以阅读更多有关决定采用BaseAdapter,而不是ArrayAdapter在这篇文章中:Android的适配器的良好实践

回答by theisenp

I'm a little late to the game, but I've run up against a similar issue.

我玩游戏有点晚了,但我遇到了类似的问题。

One way to deal with #1 would be to maintain the reference to the list within a subclass of ArrayAdapter, so that your reuse is controlled by the adapter object.

处理 #1 的一种方法是在 的子类中维护对列表的引用ArrayAdapter,以便您的重用由适配器对象控制。

Something like:

就像是:

public class DomainAdapter extends ArrayAdapter<DomainObject> {

    private final List<DomainObject> items;

    public DomainAdapter(Context context, List<DomainObject> items) {
        super(context, R.layout.mine, items);
        this.items = items;
    }

    public List<DomainObject> getItems() {
        return items;
    }
}

回答by yesennes

A quick test says that method 1 works. It seems the quickest and cleanest, but since it is undocumented you may want to test it across the intended platforms and whenever they update in case the underlying structure of ArrayAdapter changes.

快速测试表明方法 1 有效。它似乎是最快和最干净的,但由于它没有记录,您可能希望在预期的平台上测试它,并且在 ArrayAdapter 的底层结构发生变化的情况下,只要它们更新。

I am using compile SDK version 22 and min SDK Version 10.

我正在使用编译 SDK 版本 22 和最小 SDK 版本 10。

回答by emandt

The best method is to "keep a reference to the List" BUT not passing "items" variable/parameter to the Constructor:

最好的方法是“保持对列表的引用”但不将“items”变量/参数传递给构造函数:

List<DomainObject> items = new ArrayList<DomainObject>(); listAdapter = new ArrayAdapter<DomainObject>(getActivity(), R.layout.mine);

List<DomainObject> items = new ArrayList<DomainObject>(); listAdapter = new ArrayAdapter<DomainObject>(getActivity(), R.layout.mine);

In this way you only instantiate the ArrayList as an empty array and you will have to manage YOUR list by yourself.

通过这种方式,您只能将 ArrayList 实例化为一个空数组,并且您必须自己管理您的列表。

回答by jeet

I think first method is best way to do this.

我认为第一种方法是最好的方法。

I dont think, Data would be original for the Another Activity. because, You would pass items through bundle, so the object is written on bundle first and then in next Activity we read from bundle.

我不认为,数据将是另一个活动的原始数据。因为,您将通过 bundle 传递项目,因此对象首先写入 bundle,然后在下一个 Activity 中我们从 bundle 中读取。

However, if you are using some other way to pass the list, use list.clone()to create new Object, instead of passing original one.

但是,如果您使用其他方式传递列表,请使用list.clone()来创建新对象,而不是传递原始对象。