0

我有一个自定义列表适配器,它在每个列表项的 xml 中都有一个 ToggleButton,所以这是列表中两个项目的示例:

在此处输入图像描述

我使用了一个链接,因为我没有足够的声誉来发布图片。不相关的部分被模糊了。对于每个列表项的布局,ToggleButton 的 id 为“toggleButton1”,但这是一个问题,因为在我创建的名为“onTogglePress()”的 onClick 函数中,当我ToggleButton activetoggle = (ToggleButton)this.findViewById(R.id.toggleButton1);用来获取有关切换的信息时,它总是引用第一个按钮,例如是否为 isChecked()。如何单独定位每个列表项,这样我可以使用切换按钮更改与其所在的实际列表项相关的信息?

编辑:这是我的自定义列表适配器中的 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;
    }
4

0 回答 0