Disabling items in the list view

Refresh

April 2019

Views

94 time

1

I have a requirement wherein I want to disable items in the list view. Say e.g., I have 5 items in the listview out of which I want to only enable 1 item.

Note: disabling means greying out the item.

Following is my code:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,android.R.layout.simple_list_item_1, movies);

I don't want to go with the custom adapter wherein we get the getView().

Is there any other way to implement this functionality?

4 answers

1

In the adapter there is a method name isEnabled which you can override. This is called for each row like getview. The onclicklistener will only fire if this function returns true. So try doing that in your custom adapter.

@Override
    public boolean isEnabled(int position) {
        if(YOUR CONDTITION){
            return false;
        }
        return true;
    }

Without adapter:

Then you need to disable the item by getting view at specific position.

0

You can also disable item using:

final Set<Integer> disabledPositions = new HashSet<Integer>();
disabledPositions.add(positionYouWantToDisable);
disabledPositions.add(positionYouWantToDisable);
disabledPositions.add(positionYouWantToDisable);

ListView listView = new ListView(this);
listView.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> adapter, View view, int position, long arg3) {
      if(!disabledPositions.contains(position) {
         // do what you want
      }
   }
});
0

Please implement the listener for this method setOnItemSelectedListener . So you can disable any item that you want.

0

try this:

    final List<Integer> disabledItems = new ArrayList<Integer>();
    disabledItems.add(0);
    disabledItems.add(2);
    lvMovies.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            if (disabledItems.contains(arg2)) {
                Toast.makeText(getApplicationContext(), "DISABLED", Toast.LENGTH_SHORT).show();
                arg1.setEnabled(false);
            } else {
                Toast.makeText(getApplicationContext(), "NOT DISABLED", Toast.LENGTH_SHORT).show();
                arg1.setEnabled(true);
            }
        }
    });