7

I have a ListView set in my AlertDialog in Android and i'm trying to add separators as heading titles to help organize the information. I figured out how to display them... but they are still selectable by the user which isn't good.

Is there a way to disable an item to be selectable in an Android ListView? I found an method isEnabled(int position) to see if an item is enabled or not but none to see if its disabled.

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
jfisk
  • 6,125
  • 20
  • 77
  • 113

4 Answers4

9

Just Now i have implemented it

ListView list=new ListView(this);
list.getChildAt(0).setEnabled(false);
Tofeeq Ahmad
  • 11,935
  • 4
  • 61
  • 87
  • 2
    sorry, i try this, but error 08-06 20:36:29.045: E/AndroidRuntime(31116): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.imotax/com.imotax.MainActivity}: java.lang.NullPointerException – yozawiratama Aug 06 '13 at 13:38
4

override this in ur code to disable the focus.

         @Override
         public boolean isEnabled(int position) {
            return false;
           }
Randroid
  • 3,688
  • 5
  • 30
  • 55
3

I've found a lot of comments saying that

setEnabled(false)
setClickable(false)
setFocusable(false)

would work, but the answer is NO

The only workaround for this approach is doing:

view = inflater.inflate(R.layout.row_storage_divider, parent, false);
view.setOnClickListener(null);
cesards
  • 15,882
  • 11
  • 70
  • 65
  • Answer by Randroid works for me. setOnClickListener(null) works but means you cannot get back to having a listener again. Extending ArrayAdapter is the correct answer i believe. – JohnyTex Jul 14 '16 at 10:49
0

setClickable(False) works fine for me . try it once . in case problem will be persist i will share my custom adapter with you.

Shailendra Singh Rajawat
  • 8,172
  • 3
  • 35
  • 40