String options[] = new String[3];
// options[0] = "Select IVC Option";
int i =0;
IDataObject emailobject = inMemoryCache_getDataObject("EMAIL_CONTACT");
IDataObject smsobject = inMemoryCache_getDataObject("SMS_CONTACT");
IDataObject voicecallobject = inMemoryCache_getDataObject("VOICE_CONTACT");
try {
if(emailobject != null){
options [i] = "Email";
i++;
}
if(smsobject != null){
options [i] = "SMS";
i++;
}
if(voicecallobject != null){
options [i] = "VoiceCall";
i++;
}
}catch (Exception e){
}Spinner ivcoptions; ivcoptions = (Spinner)view.findViewById(R.id.spinner);
ArrayAdapter<String> x = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item,options);
x.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ivcoptions.setAdapter(x);
当且仅当Spinner包含1个项目时,我希望禁用微调器。 我试过控制字符串数组的长度。但它没有帮助。
答案 0 :(得分:1)
如果要禁用微调器项目单击事件,则需要更改适配器,但是如果您只想阻止微调器被单击,则只需在适配器上注册数据观察器:
final Spinner ivcoptions = (Spinner) findViewById(R.id.spinner);
final ArrayAdapter<String> x = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item,options);
x.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Register a data observer
x.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
if (x.getCount() == 1) {
ivcoptions.setEnabled(false);
} else {
ivcoptions.setEnabled(true);
}
}
});
// Set your adapter
ivcoptions.setAdapter(x);