简体   繁体   English

如何从Activity访问List Adapter创建的UI元素?

[英]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.

相关问题 如何从其 Recycler View Adapter 调用 Activity 的方法? 或者更好,如何从适配器访问活动的 UI 元素? - How do i call a method of an Activity from its Recycler View Adapter? Or better,How to access the UI elements of an activity from the adapter? 如何从Android中的活动访问自定义适配器内部片段的UI元素 - How can I access UI Elements of a fragment inside of a Custom Adapter From an Activity in Android 在从 Activity 调用的类中创建适配器时,如何访问 Activity 中的适配器? - How to access to an adapter in an Activity when the adapter is created in a class which is called from the Activity? 如何从适配器获取 List size() 并从回收器适配器本身更新 Activity UI? - How to get the List size() from adapter and update Activity UI from recycler Adapter itself? 如何从Main Activity中访问片段及其关联的UI元素 - How to access Fragments and their associated UI elements from Main Activity 如何从片段访问父活动中的 UI 元素 - how to access UI elements in parent activity from fragment 如何从适配器中的活动中调用元素 - How to call elements from activity in adapter 如何从活动访问适配器控件? - How to access the adapter controls from activity? 如何从适配器访问arrayList到活动? - How to access an arrayList from an adapter to an activity? 如何从适配器访问我的主要活动以及如何获取视图中元素的状态? - How can I access my main activity from an adapter and how to get the state of elements in views?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM