Switch listener in ListView giving wrong results
<p>Here is my  Array adapter. It assigns each switch button in the list with a listener that is dependant on the id given by the database (passed using <code>devices.id</code>). But for some reason, the switch listener is acting weird. </p>

<p>For debugging purpose, I'm just using the same on change listener regardless of the state. So whether I turn it ON or OFF, it runs the same function.</p>

<p>My problem is sometimes when I turn ON 1st switch, 7th switch is turned ON, n when I put ON 2nd switch, 8th switch is turned ON. There's something wrong I'm doing, but I cant really figure out. The 6th, 7th and 8th switches don't work properly.</p>

<p>You can see in the LCD, When I turn ON the switches in order, it must give me the output "12345678". But its not. Instead it gives "12345112".</p>

<p><strong>Edit: Answer given by Emil Adz works. Gives me output "12345678". But I have one problem. If I turn ON switch1, scroll down till its not visible and scroll up, the switch resets to OFF. Why is it so?</strong></p>

<p><img src="http://i.stack.imgur.com/M7VhN.png" alt="Wireless Controller Screenshot">
<img src="http://i.stack.imgur.com/6k9WW.png" alt="Wireless Controller Screenshot">
<img src="http://i.stack.imgur.com/biZ4s.jpg" alt="LCD Output"></p>

<pre><code>public class DevListAdapter extends ArrayAdapter&lt;Devices&gt;{

    Context context; 
    int layoutResourceId;    
    Devices data[] = null;
    private TextView txt = null;

    public DevListAdapter(Context context, int layoutResourceId, Devices[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d("WCAM","GetView");
        View row = convertView;
        DeviceHolder holder = null;
        Devices devices = data[position];
        String id = devices.id;

        Log.d("WCAM","Inflator");
        if(row == null)
        {
            Log.d("WCAM","True");
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new DeviceHolder();
            holder.devTxt = (TextView)row.findViewById(R.id.devdesc);
            holder.nameTxt = (TextView)row.findViewById(R.id.devname);
            holder.toggleSwitch = (Switch)row.findViewById(R.id.toggleswitch);
            holder.txt = (TextView) row.findViewById(R.id.debug);
            final int i;

            if(id.matches("1")) {
                i = 0x31;
            }
            else if(id.matches("2")) {
                i = 0x32;
            }
            else if(id.matches("3")) {
                i = 0x33;
            }
            else if(id.matches("4")) {
                i = 0x34;
            }
            else if(id.matches("5")) {
                i = 0x35;
            }
            else if(id.matches("6")) {
                i = 0x36;
            }
            else if(id.matches("7")) {
                i = 0x37;
            }
            else {
                i = 0x38;
            }
            holder.toggleSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Control._service.write(i);
                }
             });

            row.setTag(holder);
        }
        else
        {
            Log.d("WCAM","False");
            holder = (DeviceHolder)row.getTag();
        }

        holder.devTxt.setText(devices.dev);
        holder.nameTxt.setText(devices.name);
        holder.txt.setText(id);

        return row;
    }

    static class DeviceHolder
    {
        TextView devTxt;
        TextView txt;
        TextView nameTxt;
        Switch toggleSwitch;
    }
}
</code></pre>

<p>devices is an object of Devices.java</p>

<pre><code>public class Devices {
    public String dev;
    public String name;
    public String id;
    public Devices(){
        super();
    }

    public Devices(String _id, String dev, String name) {
        super();
        this.dev = dev;
        this.name = name;
        this.id = _id;
    }
}
</code></pre>
