title: Toggle button in ListView changes State on Scrolling
body: <p>I think I have searched through all the posts relating to my question but could not find a solution to my problem. I have A ListView with two textviews and one Toggle button. Toggle button Changes its state when I scroll up or down (go out of view). I am using viewholder (suggested on the forum) that does make the button to retain its state but then the recycling property comes into play and mess up my application i.e multiple toggle buttons change their state on one click.
  What I want?
  I want my toggle button to retain its previous state when it goes out of view and comes back plus when I click one toggle button only that toggle button should work.
  Here is my Code for Listview (pardon me for not putting much comments)</p>
  
  <pre><code>    public class ActivityB extends AppCompatActivity {
      ListView list;
      Button edit;
      public String text= "";
      public SharedPreferences.Editor editor;
      public SharedPreferences sharedPreferences;
  
      private ArrayList&lt;String&gt; data = new ArrayList&lt;String&gt;();
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_b);
          list = (ListView) findViewById(R.id.listView);
         generateListContent();
          list.setAdapter(new MyListadapter(this,R.layout.constraint,data));
          edit= (Button) findViewById(R.id.button);
          sharedPreferences=getSharedPreferences("MyData",Context.MODE_PRIVATE);
          editor=sharedPreferences.edit();
          text=sharedPreferences.getString("Text","Null");
          //Toast.makeText(this,text,Toast.LENGTH_LONG).show();
  
  
      }
  
     public void generateListContent() {
          for (int i = 0; i &lt; 11; i++) {
              data.add("This is item no " + i);
          }
  
      }
  
      private class MyListadapter extends ArrayAdapter&lt;String&gt; {
          private int layout;
  
          ArrayList&lt;String&gt; list;
          public MyListadapter(@NonNull Context context, @LayoutRes int resource, @NonNull List&lt;String&gt; objects) {
              super(context, resource, objects);
              layout=resource;
              list= (ArrayList&lt;String&gt;) objects;
          }
  
          @NonNull
          @Override
          public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            ViewHolder mainviewHolder=null;
              if(convertView==null){
                  LayoutInflater inflater=LayoutInflater.from(getContext());
                  convertView= inflater.inflate(layout,parent,false);
                  final ViewHolder viewHolder = new ViewHolder();
                  viewHolder.title= (TextView) convertView.findViewById(R.id.textView);
              viewHolder.lastone= (TextView) convertView.findViewById(R.id.textView2);
              viewHolder.button= (ToggleButton) convertView.findViewById(R.id.toggleButton);
  
              //mainviewHolder.title.setText(getItem(position));
                  viewHolder.button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                      @Override
                      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                              if (isChecked) {
                                  buttonView.setChecked(true);
                                  Toast.makeText(getContext(), "Button is On" + position , Toast.LENGTH_LONG).show();
                              } else {
                                  buttonView.setChecked(false);
                                  Toast.makeText(getContext(), "Button is OFF" + position + buttonView.getId(),Toast.LENGTH_LONG).show();
                              }
  
                      }
                  });
  
                  convertView.setTag(viewHolder);
              }
             else{
                 mainviewHolder = (ViewHolder) convertView.getTag();
                mainviewHolder.title.setText(getItem(position));
            }
              return convertView;
          }
      }
      public class ViewHolder{
          TextView title;
          TextView lastone;
          ToggleButton button;
      }
  
  }
  </code></pre>