title: Android Toggle Button state changes on scrolling listview
body: <p>Iam new to android . i have a toggle button in a listview and by default it should show on state !but it is showing off and on clicking the button state changes but on scrolling the view state is not retained.</p>
  
  <p>My list view look like below:
              Bill No                 1234
              Booking Date            2013-06-13
              Invoice Number          123
              Port of Loading         df
              Port of Destination     ys
              Alert                   ToggleButton unchecked ie off state</p>
  
  <p>My listviewadapter class </p>
  
  <pre><code>private class listviewAdapter extends BaseAdapter
      {
          public ArrayList&lt;HashMap&lt;String,String&gt;&gt; list;
          Activity activity;
          SparseBooleanArray mSparseBooleanArray;
  
          public listviewAdapter(Activity activity, ArrayList&lt;HashMap&lt;String,String&gt;&gt; list) 
          {
              // TODO Auto-generated constructor stub
              super();
              this.activity = activity;
              this.list = list;
              mSparseBooleanArray = new SparseBooleanArray(list.size());
          }
  
          public int getCount() {
              // TODO Auto-generated method stub
              return list.size();
          }
  
          public Object getItem(int position) {
              // TODO Auto-generated method stub
              return list.get(position);
          }
  
          public long getItemId(int arg0) {
              // TODO Auto-generated method stub
              return 0;
          }
  
  
  
          public View getView(int position, View convertView, ViewGroup parent) 
          {
              // TODO Auto-generated method stub
              ListViewHolder viewHolder;
              LayoutInflater inflater =  activity.getLayoutInflater();
              final int pos = position;
              if (convertView == null)
              {
                  convertView=inflater.inflate(R.layout.listview_row_list1,null);
                  txtFirst=(TextView) convertView.findViewById(R.id.FirstText);
                  txtSecond=(TextView) convertView.findViewById(R.id.SecondText);
                  togglbtn=(ToggleButton) convertView.findViewById(R.id.tglbtn1);
                   togglbtn.setTag(position);
                   togglbtn.setChecked(mSparseBooleanArray.get(position));
                   togglbtn.setOnCheckedChangeListener(mCheckedChangeListener);
                  convertView.setTag( new ListViewHolder(txtFirst,txtSecond,togglbtn));
  
              }
              else
              {
                  viewHolder = (ListViewHolder) convertView.getTag();
                  txtFirst = viewHolder.getText1();
                  txtSecond = viewHolder.getText2();
                  togglbtn =  viewHolder.getButton();
  
  
  
              }
  
              HashMap&lt;String, String&gt; map = list.get(position);
          //  count=count+6;
              txtFirst.setTypeface(font1);
              txtFirst.setText(map.get(FIRST_COLUMN));
              txtSecond.setTypeface(font2);
              txtSecond.setText(map.get(SECOND_COLUMN));
              String data= map.get(FIRST_COLUMN);
              String value_data=map.get(SECOND_COLUMN);
  
                  if(data.trim().equals("HBL NO"))
                  {
                      convertView.setBackgroundResource(R.color.LightBlue);
                  }
                  else
                  {
                      convertView.setBackgroundColor(Color.WHITE);
                  }
  
                  if(data.trim().equals("ALERT"))
                  {
                      togglbtn.setVisibility(View.VISIBLE);
  
                      /*if(value_data.trim().equals("YES"))
                      {
  
                          togglbtn.setChecked(true);
                      }
                      else
                      {
  
                          togglbtn.setChecked(false);
                      }*/
                  }
                  else
                  {
                      togglbtn.setVisibility(View.GONE);
                  }
  
  
  
  
  
              return convertView;
  
          }
  
       public boolean isChecked(int position) {
              return mSparseBooleanArray.get(position, false);
          }
  
          public void setChecked(int position, boolean isChecked) {
              mSparseBooleanArray.put(position, isChecked);
  
          }
  
          public void toggle(int position) {
              setChecked(position, !isChecked(position));
  
          }
  
          OnCheckedChangeListener mCheckedChangeListener=new OnCheckedChangeListener() {
  
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  // TODO Auto-generated method stub
                  System.out.println("Tag: "+buttonView.getTag());
                  mSparseBooleanArray.put((Integer) buttonView.getTag(),
                          isChecked);
                  System.out.println("Status: "+isChecked);
  
              }
          };
  
  
      }
  
      private static class ListViewHolder 
      {
          private TextView txt1 ;
          private TextView txt2 ;
          private ToggleButton btn;
          int ref;
  
          //public ListViewHolder() {}
          public ListViewHolder(TextView tx1,TextView tx2,ToggleButton btn) {
              this.txt1=tx1 ;
              this.txt2=tx2 ;
              this.btn=btn;
          }
          public TextView getText1() {
              return txt1;
          }
          public TextView getText2() {
              return txt2;
          }
  
          public ToggleButton getButton()
          {
              return btn;
          }
      }
  </code></pre>