我正在为闹钟应用程序开发贪睡功能,我正在使用微调器来选择贪睡分钟,默认情况下它选择作为第一个数组int值,但是在贪睡时间闹钟没有上升。默认情况下,它不应该选择任何值,用户需要选择。有没有其他方法可以做到这一点,这是行不通的。
spinner_snooze = (Spinner) findViewById(R.id.spinner1);
String[] array = getResources().getStringArray(R.array.snooze_time);
Integer[] intArray = new Integer[array.length];
for(int i = 0; i < array.length; i++) {
intArray[i] = Integer.parseInt(array[i]);
}
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,
android.R.layout.simple_spinner_dropdown_item, intArray);
spinner_snooze.setAdapter(adapter);
spinner_snooze.setSelected(false);
spinner_snooze.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
AlarmManager mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Integer i = (Integer) spinner_snooze.getSelectedItem();
snoozeAlarm(i, mAlarmManager, context);
// Toast.makeText(getApplicationContext(), x, Toast.LENGTH_LONG).show();
finish();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
答案 0 :(得分:0)
更改此
Integer i = (Integer) spinner_snooze.getSelectedItem();
到
Integer i = (Integer) parent.getSelectedItem();
另外添加任何默认值作为第一项。检查是否选择了第一项,而不是做任何事情。
插入一个默认值,如&#34; - &#34;并使用
进行比较if(parent.getSelectedItem().compareTo(" -- ")== 0
{
//Do Nothing
}
else
{
//Do your stuff here
AlarmManager mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Integer i = (Integer) spinner_snooze.getSelectedItem();
snoozeAlarm(i, mAlarmManager, context);
// Toast.makeText(getApplicationContext(), x, Toast.LENGTH_LONG).show();
finish();
}