私は Android プラットフォームは初めてですが、Java は初めてです。
問題:
ArrayAdapter にある ToggleButton を選択するたびに、複数の 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;
}
リストを移動するたびにトグルボタンが再描画される理由がわかりません。