Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to disable list items on list creation

Tags:

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

like image 403
Martyn Avatar asked Feb 02 '10 11:02

Martyn


People also ask

How do I clear Android list?

Call clear() method from your custom adapter . clean() is not available with BaseAdapter. you can also set the listview adapter to null.

What is list and custom list in Android?

Android Custom ListView (Adding Images, sub-title)After creating simple ListView, android also provides facilities to customize our ListView. As the simple ListView, custom ListView also uses Adapter classes which added the content from data source (such as string array, array, database etc).

What is a list in Android?

Android ListView is a view which groups several items and display them in vertical scrollable list. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database.


2 Answers

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     } } 
like image 89
Viktor Brešan Avatar answered Sep 21 '22 14:09

Viktor Brešan


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.

like image 45
Matthias Avatar answered Sep 21 '22 14:09

Matthias