我有一个自定义列表适配器,在xml中为每个列表项都有一个ToggleButton,所以这是列表中两个项目的示例:
我使用了链接,因为我没有足够的声誉来发布图片。不相关的部分模糊了。对于每个列表项的布局,ToggleButton的id为“toggleButton1”,但这是一个问题,就像我在onTogglePress()中创建的onClick函数一样,当我使用ToggleButton activetoggle = (ToggleButton)this.findViewById(R.id.toggleButton1);
时,它总是引用第一个获取有关切换按钮的信息,例如是否为check()。我如何单独定位每个列表项,以便我可以使用切换按钮来更改与其所在的实际列表项相关的信息?
编辑:这是我的自定义列表适配器中的getView()类,如下所示:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView; //this is listview_item_row
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ListHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.txtTime = (TextView)row.findViewById(R.id.txtTime);
holder.txtDays = (TextView)row.findViewById(R.id.txtDays);
holder.txtVibrate = (TextView)row.findViewById(R.id.txtVibrate);
holder.activButton = (ToggleButton)row.findViewById(R.id.toggleButton1);
row.setTag(holder);
}
else
{
holder = (ListHolder)row.getTag();
}
ListObject currentobj = storagelist.returnSchedule(position);
holder.txtTitle.setText(currentobj.returnName());
holder.txtTime.setText(currentobj.timestostring());
holder.txtDays.setText(currentobj.daystostring());
holder.txtVibrate.setText(currentobj.vibratetostring());
holder.activButton.setChecked(currentobj.isActive());
Typeface customfont = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Light.ttf");
holder.txtTitle.setTypeface(customfont);
holder.txtTime.setTypeface(customfont);
holder.txtDays.setTypeface(customfont);
holder.txtVibrate.setTypeface(customfont);
if(currentobj.returnVibrate())
holder.txtVibrate.setTextColor(Color.parseColor("#35b4e3")); //green
else
holder.txtVibrate.setTextColor(Color.parseColor("#8C8C8C")); //red
return row;
}