Abhinav
Abhinav

Reputation: 752

how to make some items of a listview non clickable?

I have following code which is quite easy to understand. I want some specific child of this listview to be non clickable.

    ArrayAdapter<String> adapter =
      new ArrayAdapter<String>(this,R.layout.list_item,R.id.module_name_item, testdata);

    m_listview.setAdapter(adapter);


    Log.i("check","1");
    if(sectionAttempts.get(0).equals("0"))
    {

        m_listview.getChildAt(2).setEnabled(false);
        m_listview.getChildAt(3).setEnabled(false);
    }
    else
    {

        if(sectionAttempts.get(2).equals("0"))
        {

            m_listview.getChildAt(3).setEnabled(false);
        }


    }

I am getting error in

  m_listview.getChildAt(2).setEnabled(false);

as java.lang.NullpointerException. I tried to find the error and used Log.i("check",m_listview.getChildCount()); And it show 0. so i am guessing the listview hasnt beent created yet!! How is that possible.

What seems to be the problem? Thanx for any help in advance.

Upvotes: 5

Views: 6905

Answers (3)

knvarma
knvarma

Reputation: 974

Override areAllItemsSelectable() method. Return false to say all the items are not clickable.

@Override
public boolean areAllItemsSelectable() {
    return false;
}

And then Override below method to return which item is not clickable

@Override
public boolean isEnabled(int position) {
    if(position == your_item_pos) {
        return false;
    }
    return true;
}

Upvotes: 16

AlikElzin-kilaka
AlikElzin-kilaka

Reputation: 35991

Sharing my experience, the following did the trick (view refers to the list item view):

view.setEnabled(false);
view.setOnClickListener(null);
  • enabling by overriding the method didn't work as the method was never invoked.
  • setting focusable to false didn't work as well.

Upvotes: 7

slezadav
slezadav

Reputation: 6141

Seems as if you didn't initilize your ListView. In onCreate Use m_listview=getListView() if you are using ListActivity or m_listview=findViewById(R.id...) if you are using regular activity.

Upvotes: 0

Related Questions