我是 Android 平台的新手,但不是 java 的新手。
问题:
每次我选择 ToggleButton,whitch 都在 ArrayAdapter 中,然后选择了一个以上的 ToggleButton。
private Context context;
private List<DayListItem> list;
public DayListAdapter(Context context, List<DayListItem> objects) {
super(context, R.layout.day_row, objects);
this.context = context;
this.list = objects;
}
@Override
public int getPosition(DayListItem item) {
return list.indexOf(item);
}
@Override
public int getCount() {
return list!=null?list.size():0;
}
static class ViewHolder{
protected RelativeLayout rl;
protected TextView position;
protected TextView time;
protected TextView info;
protected ToggleButton button;
}
@Override
public DayListItem getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return list.get(position).getID();
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View view = convertView;
final ViewHolder viewHolder;
if(view == null){
LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflate.inflate(R.layout.day_row, null);
viewHolder = new ViewHolder();
viewHolder.rl = (RelativeLayout) view.findViewById(R.id.alarmCLICK);
viewHolder.position = (TextView) view.findViewById(R.id.alarmPOSITION);
viewHolder.time = (TextView) view.findViewById(R.id.alarmSETtime);
viewHolder.info = (TextView) view.findViewById(R.id.alarmINFO);
viewHolder.button = (ToggleButton) view.findViewById(R.id.alarmTOGGLE);
view.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.time.setText(list.get(position).getTime());
viewHolder.info.setText(list.get(position).getInfo());
viewHolder.position.setText(String.valueOf(position));
viewHolder.button.setSelected(list.get(position).isSelected());
viewHolder.rl.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Dialog dialog = new Dialog(v.getContext());
dialog.setContentView(R.layout.alarm_list);
dialog.setTitle(String.valueOf(position));
dialog.setCancelable(true);
dialog.show();
}
});
viewHolder.button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(viewHolder.button.isChecked()){
Log.w("position",String.valueOf(position));
list.get(position).setSelected(true);
}else list.get(position).setSelected(false);
}
});
return view;
}
我不知道为什么每次移动列表时都会重新绘制 ToggleButtons。