Android 闹钟 开发过程记录(四)

闹钟列表的界面,listview中每个item都有个checkbox控件,并且它的状态还控制着一个textview的显示内容。

当添加闹钟大于listview长度,,滑动时,checkbox的状态会出现混乱的情况,包括checkbox所控制的控件如TextView,也会出现复用错位。

解决办法:

在适配器的构造方法中 就将所有闹钟的状态拿出来,存进List集合中。

在getview()方法中通过指定的position,获取闹钟的状态,然后设置checkbox的状态和它控制的TextView的文本。

checkbox的状态变化时,记住要在在checkbox的监听事件里,将之前存进集合的对应的状态也改下。

适配器中的代码如下:

public class AlarmClockAdapter extends BaseAdapter {private static final String TAG = "AlarmClockAdapter";private Context context;private List<AlarmClockInfo> alarmClockInfos;private List<Integer> statePosition ; //闹钟状态的集合 public AlarmClockAdapter(Context context, List<AlarmClockInfo> alarmClockInfos) {this.context = context;this.alarmClockInfos = alarmClockInfos;statePosition = new ArrayList<Integer>(alarmClockInfos.size());//在构造方法里把闹钟的 状态 存放进集合 目的:解决checkbox和它所控制的textview,在滑动时的复用错位for (AlarmClockInfo clockInfo : alarmClockInfos) {statePosition.add(clockInfo.getState());}}public void setAlarmClockInfos(List<AlarmClockInfo> alarmClockInfos){this.alarmClockInfos = alarmClockInfos;}@Overridepublic int getCount() {return alarmClockInfos.size();}@Overridepublic Object getItem(int position) {return alarmClockInfos.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(final int position, View convertView, ViewGroup parent) {final AlarmClockInfo clockInfo = alarmClockInfos.get(position);String hour = clockInfo.getHour()+"";String minute = clockInfo.getMinute()+"";String tag = clockInfo.getTag();String repeatCycle = clockInfo.getRepeatCycle();final ViewHolder holder = new ViewHolder();View view;if(convertView == null){//Log.i(TAG, "位置 "+position+" ——————- ———-创建了convertview对象");view = View.inflate(context, R.layout.alarm_clock_item_activity, null);}else{view = convertView;//Log.i(TAG, "位置 "+position+" ——————- 复用了convertview对象");}holder.showTime = (TextView) view.findViewById(R.id.tv_alarm_clock_time);holder.showRepeatCycle = (TextView) view.findViewById(R.id.tv_alarm_clock_repeat);holder.showTag = (TextView) view.findViewById(R.id.tv_alarm_clock_tag);holder.showLeftTime = (TextView) view.findViewById(R.id.tv_alarm_clock_lefttime);holder.line = view.findViewById(R.id.line);holder.cb_isOpen = (CheckBox) view.findViewById(R.id.cb_alarm_clock_isopen);if(hour.length() == 1){hour = "0"+hour;}if(minute.length() == 1){minute = "0"+minute;}holder.showTime.setText(hour+":"+minute);holder.showRepeatCycle.setText(repeatCycle);// 当ListView滚动时自动调用 onCheckedChanged 导致CheckBox 状态不停变化 (如同见鬼一样令人费解)//解决办法:在初始化CheckBox状态 和 设置状态变化监听事件之前,//先把状态变化监听事件设置为null holder.checkBox.setOnCheckedChangeListener(null);holder.cb_isOpen.setOnCheckedChangeListener(null);int dTime = NextRingTimeProvider.showRingTimeByRepeatCycle(repeatCycle, clockInfo.getHour(), clockInfo.getMinute());String ringTime = RingTextUtil.showRingTime(dTime);holder.cb_isOpen.setChecked(statePosition.get(position)==0?false:true);holder.showLeftTime.setText(statePosition.get(position)==0?"未开启":ringTime);if(TextUtils.isEmpty(tag)){holder.line.setVisibility(View.GONE);holder.showTag.setVisibility(View.GONE);}else{holder.line.setVisibility(View.VISIBLE);holder.showTag.setVisibility(View.VISIBLE);holder.showTag.setText(tag);}holder.cb_isOpen.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {AlarmClockDao dao = new AlarmClockDao(context);AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);Intent alarmIntent = new Intent(context, AlarmClockReceiver.class);alarmIntent.setAction("cn.edu.usts.cardhelper.alarmclock");Bundle bundle = new Bundle();bundle.putInt("alarmClockId", clockInfo.getId());bundle.putString("repeatCycle",clockInfo.getRepeatCycle());bundle.putInt("hour", clockInfo.getHour());bundle.putInt("minute", clockInfo.getMinute());bundle.putString("ringInfo", clockInfo.getRingInfo());bundle.putInt("isShake", clockInfo.getShake());bundle.putString("tag", clockInfo.getTag());alarmIntent.putExtras(bundle);if(isChecked){//选中状态–> 1.更新数据库 2.发送广播dao.updataState(clockInfo.getId(), 1);int dTime = NextRingTimeProvider.showRingTimeByRepeatCycle(clockInfo.getRepeatCycle(), clockInfo.getHour(), clockInfo.getMinute());PendingIntent pi = PendingIntent.getBroadcast(context, clockInfo.getId(), alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+dTime, pi);Log.i(TAG, "–广播发送成功!–ID:"+clockInfo.getId()+"–repeatCycle"+clockInfo.getRepeatCycle()+"–hour:"+clockInfo.getHour()+"—minute:"+clockInfo.getMinute()+"—铃声信息:"+clockInfo.getRingInfo()+"—振动:"+clockInfo.getShake()+"—tag:"+clockInfo.getTag());String ringTime = RingTextUtil.showRingTime(dTime);holder.showLeftTime.setText(ringTime);ToastUtil.showRingTimeToast(context, dTime);if(statePosition.get(position)==0){statePosition.set(position, 1);}}else{//未选中状态 –> 1.更新数据库 2.取消广播dao.updataState(clockInfo.getId(), 0);holder.showLeftTime.setText("未开启");alarmManager.cancel(PendingIntent.getBroadcast(context, clockInfo.getId(), alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT));Log.i(TAG, "–广播取消成功!–ID:"+clockInfo.getId()+"–repeatCycle"+clockInfo.getRepeatCycle()+"–hour:"+clockInfo.getHour()+"—minute:"+clockInfo.getMinute()+"—铃声为:"+clockInfo.getRingInfo()+"—振动:"+clockInfo.getShake()+"—tag:"+clockInfo.getTag());if(statePosition.get(position)==1){statePosition.set(position, 0);}}}});return view;}private static class ViewHolder{private TextView showTime;private TextView showRepeatCycle;private TextView showLeftTime;private TextView showTag;private View line;private CheckBox cb_isOpen;}}

人生就是一场旅行,不在乎目的地,

Android 闹钟 开发过程记录(四)

相关文章:

你感兴趣的文章:

标签云: