How to make some list items without clicks?

I have the following code that is pretty easy to understand. I want some particular child of this list not to be 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 get an error in

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

like java.lang.NullpointerException. I tried to find the error and used Log.i ("check", m_listview.getChildCount ()); And it shows 0., so I guess the listview has not been created yet! How is this possible.

What is the problem? Thanx for any help in advance.

+5
source share
3 answers

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

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

And then override the method below to return the element that cannot be clicked

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

, ( ):

view.setEnabled(false);
view.setOnClickListener(null);
  • , .
  • false, .
+7

It seems that you did not initialize the ListView. In onCreateuse m_listview=getListView(), if you use ListActivity or m_listview=findViewById(R.id...)if you are using a regular activity.

0
source

All Articles