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
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)andareAllItemsEnabled(). In former you returntrueorfalsedepending is list item at given position enabled or not. In latter you returnfalse.If you want to use
createFromResource()you will have to implement that method as well, since theArrayAdapter.createFromResource()still instantiatesArrayAdapterinstead of your own adapter.Finally, the code would look something like the following: