Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 636
Abhinav Avatar asked Sep 14 '12 13:09

Abhinav


2 Answers

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;
}
like image 127
knvarma Avatar answered Oct 23 '22 14:10

knvarma


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.
like image 39
AlikElzin-kilaka Avatar answered Oct 23 '22 12:10

AlikElzin-kilaka