做小闹钟项目时,刚坐到添加闹钟时出现了问题,在AVD中调试的时候,添加一次闹钟,却出现两个相同的闹钟 import java.util.Calendar; import java.util.Date; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TimePicker; public class AlarmView extends LinearLayout { private Button btnAddAlarm; private ListView lvAlarmList; private ArrayAdapter<AlarmData> adapter; public AlarmView(Context context) { super(context); } public AlarmView(Context context, AttributeSet attrs) { super(context, attrs); } public AlarmView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onFinishInflate() { super.onFinishInflate(); btnAddAlarm = (Button) findViewById(R.id.btnAddAlarm); lvAlarmList = (ListView) findViewById(R.id.lvAlarmList); adapter = new ArrayAdapter<AlarmView.AlarmData>(getContext(), android.R.layout.simple_list_item_1); lvAlarmList.setAdapter(adapter); btnAddAlarm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addAlarm(); } }); } private void addAlarm(){ Calendar c = Calendar.getInstance(); new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Calendar setTime = Calendar.getInstance(); setTime.set(Calendar.HOUR_OF_DAY, hourOfDay); setTime.set(Calendar.MINUTE, minute); Calendar currentTime = Calendar.getInstance(); if(setTime.getTimeInMillis() <= currentTime.getTimeInMillis()){ setTime.setTimeInMillis(setTime.getTimeInMillis()+24*60*60*1000); } adapter.add(new AlarmData(setTime.getTimeInMillis())); } }, c.get(Calendar.HOUR_OF_DAY), Calendar.MINUTE, true).show(); } private static class AlarmData{ private long time = 0; private String timeLabel = ""; private Calendar date; public AlarmData(long time) { this.time = time; date = Calendar.getInstance(); date.setTimeInMillis(time); timeLabel = String.format("%d月%d日 %d:%d", date.get(Calendar.MONTH)+1, date.get(Calendar.DAY_OF_MONTH), date.get(Calendar.HOUR_OF_DAY), date.get(Calendar.MINUTE)); } public long getTime() { return time; } public String getTimeLabel() { return timeLabel; } @Override public String toString() { return getTimeLabel(); } } } |
|
50分 |
adapter的数据源是什么,看看是不是初始时,里面就有数据了,这样在执行了addalarm后,又添加了一条
|
不是的,我在onFinishInflate()里面初始化了一个闹钟
Calendar cc = Calendar.getInstance(); adapter.add(new AlarmData(cc.getTimeInMillis())); 结果就出现一个,说明不是adapter的问题。 |
|
问题已经解决了,是android4.1和4.2的系统bug。需要重写TimePickerDialog中的onStop方法
tpd = new TimePickerDialog(getContext(), otsl, hourOfDay, min, true){ @Override protected void onStop() { //super.onStop(); } }; 把里面的super.onStop()注释掉就行了。 |
|
具体解决方法可以和我说说吗,本人安卓小白,加我QQ525919113
|
|
求指点具体操作,加我QQ525919113 |