如何访问从一个活动列表适配器创建UI元素? [英] How to access UI elements created by a List Adapter from an Activity?
问题描述
在我现在的code,我有一个DetailActivity,它只是调用ListAdapter类来填充一个ListView。
In my code now, I have a DetailActivity, which simply calls a ListAdapter class to populate a ListView.
在ListAdapter类,我夸大了一些自定义视图,其中一些含有按钮。
Within the ListAdapter class, I am inflating some custom views, some of which contain Buttons.
返回从我DetailActivity内,我希望能够访问这些按钮启用/禁用它们根据特定用户操作。有没有办法做到这一点?
Back from within my DetailActivity, I would like to be able to access these buttons to enable/disable them depending on certain user actions. Is there a way to do this?
我想更大的问题是:从活动中,我怎么能抓住任何一个元素的引用(按钮,imageviews,textviews等),这些从适配器创建
I guess the larger question is: from an Activity, how can I grab a reference to any element (buttons, imageviews, textviews,etc) that are created from an Adapter?
感谢您!
推荐答案
我假设你有一个列表<对象>
,它通过ListAdapter的构造函数发送。
只需添加一个布尔isEnable
到对象
,然后在你的 getView()
方法,添加此行:
I assume you have a List<Object>
that is sent through the constructor of ListAdapter.
Just add a boolean isEnable
to the Object
, and then in your getView()
method, add this line:
button.setEnabled(getItem(position).isEnable);
在你的 DetailActivity
,您可以更改 isEnable
如你所愿。要记住, adapter.notifyDataSetChanged()
来得到它的工作。
In your DetailActivity
, you can changeisEnable
as you wish. And remember to adapter.notifyDataSetChanged()
to get it worked.
这篇关于如何访问从一个活动列表适配器创建UI元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!