[英]How to access UI elements created by a List Adapter from an Activity?
In my code now, I have a DetailActivity, which simply calls a ListAdapter class to populate a ListView. 在我的代码中,我有一个DetailActivity,它只是调用ListAdapter类来填充ListView。
Within the ListAdapter class, I am inflating some custom views, some of which contain Buttons. 在ListAdapter类中,我正在膨胀一些自定义视图,其中一些包含按钮。
Back from within my DetailActivity, I would like to be able to access these buttons to enable/disable them depending on certain user actions. 从我的DetailActivity中返回,我希望能够访问这些按钮以启用/禁用它们,具体取决于某些用户操作。 Is there a way to do this?
有没有办法做到这一点?
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? 我想更大的问题是:从一个Activity,我如何获取从适配器创建的任何元素(按钮,图像视图,文本视图等)的引用?
Thank you! 谢谢!
I assume you have a List<Object>
that is sent through the constructor of ListAdapter. 我假设您有一个
List<Object>
,它通过ListAdapter的构造函数发送。 Just add a boolean isEnable
to the Object
, and then in your getView()
method, add this line: 只需在
Object
添加一个boolean isEnable
,然后在你的getView()
方法中添加以下行:
button.setEnabled(getItem(position).isEnable);
In your DetailActivity
, you can change isEnable
as you wish. 在您的
DetailActivity
,您可以根据需要更改isEnable
。 And remember to adapter.notifyDataSetChanged()
to get it worked. 并记住
adapter.notifyDataSetChanged()
以使其工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.