How to disable the particular list item in list-view in android?

后端 未结 6 746
陌清茗
陌清茗 2021-02-05 13:40

How to disable the particular list item in list-view in android? I mean if once i selected any one of item from a list-view,that item suppose to be disabled which means that ite

6条回答
  •  天命终不由人
    2021-02-05 14:26

    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)

    class MenuAdapter extends ArrayAdapter {
    
    public boolean isEnabled(int position) {
       // return false if position == positionYouWantToDisable
    }
    

    }

    Or in Activity class

    public class MainActivity extends Activity {
    
    ListView listview;
    ArrayAdapter arrayadapter;
    
    @Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
    
     listview = (ListView)findViewById(R.id.listView1);
     button = (Button)findViewById(R.id.button1);
    
     arrayadapter = new ArrayAdapter(MainActivity.this, 
     android.R.layout.simple_list_item_1, subjects);
    
     listview.setAdapter(arrayadapter);
     listview.getChildAt(1).setEnabled(false);
     }
    

    }

提交回复
热议问题