views:

1310

answers:

2

Hi,

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

+3  A: 

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.

Matthias
Thanks for your reply, this looks good but having issues implementing it.setListAdapter(MenuAdapter.createFromResource(this, R.array.mainMenu,android.R.layout.simple_list_item_1));MenuAdapter mainMenuList = (MenuAdapter) getListAdapter();mainMenuList.isEnabled(2); //just set to return false atmAny ideas where I'm going wrong? it currently just crashes
Martyn
Well, what is the exception you're getting?
Matthias
02-03 13:51:21.971: ERROR/AndroidRuntime(1365): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.martyn.test/com.martyn.test.test}: java.lang.ClassCastException: android.widget.ArrayAdapter
Martyn
on which line? setListAdapter(MenuAdapter.createFromResource(this, R.array.mainMenu,android.R.layout.simple_list_item_1));? That's weird, because createFromResource returns an ArrayAdapter, and an ArrayAdapter is a ListAdapter. Try instantiating it manually instead of using the resource helper then.
Matthias
+1  A: 

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
    }
}
Viktor Bresan