获取自定义适配器中的项目列表 [英] Get list of items in custom adapter
问题描述
所以,我有一个扩展ArrayAdapter的自定义适配器,我想从适配器中获取整个列表,可以这样做吗?
So, I have a custom adapter extending ArrayAdapter, and I wanna get the entire list from within the adapter, is it possible to do that?
我知道在ArrayAdapter构造函数中,您传入了对象列表,并将其存储为mObjects,但是该列表是私有的.我想到了只在构造函数中的自定义适配器中创建自己的列表,但无法通过adapter.add(...)方法更改它.有没有办法访问最新列表?还是有什么方法可以更新在自定义适配器的构造函数中初始化的对象列表?
I know that in the ArrayAdapter constructor, you pass in the list of objects and it stores it as mObjects, but that list is private. I thought of just creating my own list in my custom adapter in the constructor but it cannot get changed by the adapter.add(...) method. Is there a way to access the up-to-date list? Or is there any way i can update the list of objects i initialised in my custom adapter's constructor?
帮助!
推荐答案
我不确定这是否行得通,因此宁愿以注释的形式将其添加为建议,但它太冗长了.您可以尝试这样的事情
I am not sure this works so would have rather added it as a suggesting in the form of a comment, but it was too lengthy. You could try something like this
List<T> items = new ArrayList<T>();
for(int i = 0; i < adapter.getCount(); i++){
items.add(adapter.getItem(i));
}
或者您应该能够在适配器中为其包含的列表创建一个简单的吸气剂
Or you should be able to create a simple getter in your adapter for the list it contains
public T getList(){
return mList;
}
- 请注意,T是您正在使用的商品类型的通用表示形式
似乎,如果您有适配器,并且方法设置正确,则应该可以正常工作.同样,不确定它是否确实存在,并且可能有更好的方法.只是以为我会很快把我的2美分扔掉.如果仍然无法解决,请告诉我,我将删除答案,以免引起其他用户的困惑.
It seems that if you have your adapter and it's methods setup correctly that should work. Again, not sure if it actually does and there is likely a better method. Just thought I would toss in my 2 cents really quick. If it doesn't work let me know and I will delete the answer to avoid confusion for other users.
这篇关于获取自定义适配器中的项目列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!