java – Android: How to disable list items on list creation-ThrowExceptions

Exception or error:

I’m pretty new to Android dev and still working out a lot of things.

I’ve got a main menu showing using the following code, but can’t work out how to disable selected items in the menu. Can anybody help me with some sample code?

public class listTest extends ListActivity {

    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        setListAdapter(ArrayAdapter.createFromResource(this, R.array.mainMenu,
                android.R.layout.simple_list_item_1)); 
        //not sure how to disable list items here
    }

    protected void onListItemClick(ListView list, View view, int position, long id) {
        // can disable items when they are clicked on
        view.setEnabled(false);
    }   

}

and I have a string-array in my strings.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="mainMenu">
        <item>Item 1</item>
        <item>Item 2</item>
        <item>Item 3</item>
    </string-array> 
</resources>

Thank you

How to solve:

In order to disable list items on list creation you have to subclass from ArrayAdapter. You have to override the following methods: isEnabled(int position) and areAllItemsEnabled(). In former you return true or false depending is list item at given position enabled or not. In latter you return false.

If you want to use createFromResource() you will have to implement that method as well, since the ArrayAdapter.createFromResource() still instantiates ArrayAdapter instead of your own adapter.

Finally, the code would look something like the following:

class MenuAdapter extends ArrayAdapter<CharSequence> {

    public MenuAdapter(
            Context context, int textViewResId, CharSequence[] strings) {
        super(context, textViewResId, strings);
    }

    public static MenuAdapter createFromResource(
            Context context, int textArrayResId, int textViewResId) {

        Resources      resources = context.getResources();
        CharSequence[] strings   = resources.getTextArray(textArrayResId);

        return new MenuAdapter(context, textViewResId, strings);
    }

    public boolean areAllItemsEnabled() {
        return false;
    }

    public boolean isEnabled(int position) {
        // return false if position == position you want to disable
    }
}

###

I believe whether a list item is enabled or not is part of that item’s state, so I guess you have to manage that in your ListAdapter. When subclassing an adapter, you can override isEnabled(position). For any position you return true here, the ListView will mark this item as disabled.

So what you want to do is something like this:

class MenuAdapter extends ArrayAdapter<String> {

    public boolean isEnabled(int position) {
       // return false if position == positionYouWantToDisable
    }

}

This probably requires e.g. a Map managing the enabled state of each item if you want to be able to enable/disable an item using a setter.

Then set the custom adapter on your ListView.

###

You can disable a list item (= make it not respond to touches) by calling both

setClickable(false)

and

setFocusable(false)

in your adapter, for example.

By default, this is not automatically reflected graphically, though.

I am currently using that in a list whose list items are not clickable but most of which contain clickable widgets. Works well.

This way, list items are drawn normally including the separator (see Janusz’ reply to the accepted answer above).

###

Or in simple way to un-register and register OnItemClickListener can be a better idea.

To register:

lstDevice.setOnItemClickListener(context);

To de-register:

lstDevice.setOnItemClickListener(null);

Leave a Reply

Your email address will not be published. Required fields are marked *