31
votes

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!

7

7 Answers

95
votes

In your custom ArrayAdapter use isEnabled function to return false:

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

always works for me.

16
votes

all the easier! list.setEnabled(false)

9
votes

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>
2
votes

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

2
votes

Try setting these two attributes on the ListView:

android:listSelector="@android:color/transparent" 
android:cacheColorHint="@android:color/transparent"
0
votes

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

0
votes

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);
    }