0

I am trying to enable/disable items from a list view when a button is pressed using getChild function inside button callback. But i found that getChild function only returns the items that are visible.

ListView.getChildAt(position).setEnabled(isEnabled);

Tried solution provided here. But unfortunately, it doesn't work in my case.

Any help is greatly appreciated.

BhanuKiran
  • 2,631
  • 3
  • 20
  • 36
  • That's something that should be handled in the `Adapter`, not externally through the `View` hierarchy. What kind of `Adapter` are you using? – Mike M. Aug 09 '19 at 08:36
  • I am using ArrayAdaptor.....And this is how it looks ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.rooms_list_item, displayList); – BhanuKiran Aug 09 '19 at 08:50
  • OK, which exact effect are you wanting? That is, do you want the items not clickable? Or just grayed out? Or both? – Mike M. Aug 09 '19 at 09:20
  • Sorry, I totally forgot about this before I left for a week. Hopefully, you got something figured out, but if not, [here's a basic example](https://drive.google.com/file/d/10PWK6ByySQ2v4UBMRV_yO8cwBUhPdngC/view?usp=drivesdk) of a custom `Adapter` that disables every odd position, for a demo. You've to override `isEnabled()` in the `Adapter` to appropriately en/disable the `ListView`'s `OnItemClickListener`, if using one. Then you've to call `setEnabled()` appropriately on the `View` in `getView()` to get the proper visual style. Just text-replace `ArrayAdapter` in your code w/ `CustomAdapter`. – Mike M. Aug 16 '19 at 07:03
  • Beyond that, I'm not sure how you mean to handle setting each item, but [here's a quick example I threw together](https://drive.google.com/file/d/10dSH9smQfVgwxibKlvqm4eghC3teCZaS/view?usp=drivesdk) that let's you set each item disabled individually, without needing a custom class with a `boolean` for each object. The logic's kinda backwards, since the default state is enabled, so you're setting the _disabled_ state; e.g., `setItemDisabled(1, true)` disables position 1. Again, it's a drop-in replacement, so just change `ArrayAdapter` to `CustomAdapter` in your code. (Not fully tested, btw.) – Mike M. Aug 16 '19 at 07:03

0 Answers0