32

I have a ListView that I'm populating with values from my database. If the database is empty, I'm setting the first item of the ListView to "No data.". I want to disable clicking on this item. I've used ArrayAdapter. I tried making areAllItemsEnabled,isEnabled false, but it was of no use. Even if I set the ListView's isClickable and setEnabled to false, it is of no use. And I put the code for the OnItemClickListener in the else condition,even that doesn't stop the list item from being clickable. Does someone have an alternate solution? Thanks!

user1625971
  • 499
  • 2
  • 5
  • 15

7 Answers7

97

In your custom ArrayAdapter use isEnabled function to return false:

@Override
public boolean isEnabled(int position) {
    return false;
}

always works for me.

yahya
  • 4,810
  • 3
  • 41
  • 58
  • How I can re-enable after disable ? – Kirtikumar A. Oct 16 '13 at 04:25
  • 3
    I would suggest you to put a flag there, and change that flag whenever you need. – yahya Oct 16 '13 at 09:27
  • It's working. We have add this code in listAdapter class in java. – gnganapath Feb 05 '15 at 13:15
  • This didn't work for me. But this answer: http://stackoverflow.com/a/2908499/956975 did work for me. – marienke Aug 30 '16 at 10:52
  • That's not the same thing @marienke it is still clickable but not visible that it's clicked. – yahya Aug 30 '16 at 17:45
  • 1
    Just to elaborate on yahya, just use the flags. Create a small function in your adapter that takes a Boolean argument. Set a global variable inside your adapter and then use it as your flag. It's a really nice solution. Good job yahya :) – CardDeath Jan 11 '17 at 13:42
17

all the easier! list.setEnabled(false)

Master
  • 690
  • 6
  • 18
9

You can set the empty View as showed and it will be handled automatically:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:paddingLeft="8dp"
         android:paddingRight="8dp">

     <ListView android:id="@android:id/list"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:background="#00FF00"
               android:layout_weight="1"
               android:drawSelectorOnTop="false"/>

     <TextView android:id="@android:id/empty"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:background="#FF0000"
               android:text="No data"/>
 </LinearLayout>
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • 2
    This should be the accepted answer! It's not only answering the question but it suggests a better way to handle an empty list, too! – chuky Aug 01 '14 at 21:13
3

create Adapter for that list, and there override this method

public boolean isEnabled(int position);

then return false when you want to disable the click

Ben
  • 3,832
  • 1
  • 29
  • 30
3

Try setting these two attributes on the ListView:

android:listSelector="@android:color/transparent" 
android:cacheColorHint="@android:color/transparent"
Nick Wright
  • 1,403
  • 13
  • 19
0

Perhaps you could use an if statement to check the contents of the listview entry in the OnClick, if it contains 'No data' do nothing, else do the usual

Broak
  • 4,161
  • 4
  • 31
  • 54
0

I did like this according to my requirement hope it can help you some how

@Override

public boolean isEnabled(int position) {
        if(data.get(position).isClickable==false)
        {
            return false;
        }
        return super.isEnabled(position);
    }
nsgocev
  • 4,390
  • 28
  • 37
DropAndTrap
  • 1,538
  • 16
  • 24