title: setOnCheckedChangeListener for checkbox in fragment
body: <p>so I'm quite new on android development and I'm here to learn. I have been doing some tutorials online for listviews.</p>
  
  <p>I'm on this one now: <a href="http://www.survivingwithandroid.com/2013/02/android-listview-adapter-checkbox-item_7.html" rel="nofollow">http://www.survivingwithandroid.com/2013/02/android-listview-adapter-checkbox-item_7.html</a></p>
  
  <p>Now, I have followed the tutorial, but I have added a little twist of my own. Instead of doing everything in <code>MainActivity</code> as the tutorial says, I have used a fragment to inflate my views, register for context menu etc. The <code>fragment</code> is an <strong>inner class</strong> within my <code>MainActivity.class</code>.</p>
  
  <p>I'm at the point of the tutorial where I need to add in a <code>CheckBox</code> in the <code>listview</code>. This seems fairly easy, so I go and implement the <code>OnCheckedChangeListener</code> inside my <code>fragment</code>class.</p>
  
  <pre><code>public class MainActivity extends ActionBarActivity  {  
  //PlaceholderFragment is an inner class of MainActivity
      public static class PlaceholderFragment extends Fragment implements android.widget.CompoundButton.OnCheckedChangeListener {
  
              ListView lv;
              public PlaceholderFragment() {
              }
  
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  // TODO Auto-generated method stub
                   int pos = lv.getPositionForView(buttonView);
                   System.out.println("Pos ["+pos+"]");
  
  
           if (pos != ListView.INVALID_POSITION) {
                   Planet p = planetsList.get(pos);         
                   p.setChecked(isChecked);
          }
          }
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.fragment_main, container,
              false);
      ListView lv = (ListView) rootView.findViewById(R.id.listView);
      aAdpt = new PlanetAdapter(planetsList, getActivity());
      lv.setAdapter(aAdpt);
      registerForContextMenu(lv);
      lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  
           public void onItemClick(AdapterView&lt;?&gt; parentAdapter, View view, int position,
                                   long id) {
  
  
               Toast.makeText(getActivity(), "Item with id ["+id+"] - Position ["+position+"] - Genre ["+aAdpt.getItem(position).getDescr()+"]", Toast.LENGTH_SHORT).show();
           }
      });
      return rootView;
  }
  </code></pre>
  
  <p>In my PlanetAdapter.class in getView method I have the following code (as per the tutorial):</p>
  
  <pre><code>public View getView(int position, View convertView, ViewGroup parent) {
      View v = convertView;
  
      PlanetHolder holder = new PlanetHolder();
  
  
      if (convertView == null) {
          LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          v = inflater.inflate(R.layout.checkbox_layout, parent, false);
  
          TextView tv = (TextView) v.findViewById(R.id.name);
          TextView distView = (TextView) v.findViewById(R.id.dist);
          CheckBox chk = (CheckBox) v.findViewById(R.id.chk);
  
          holder.chk = chk;
          holder.planetNameView = tv;
          holder.distView = distView;
          **chk.setOnCheckedChangeListener((MainActivity) context);** 
          v.setTag(holder);
      }
      else 
          holder = (PlanetHolder) v.getTag();
  
      Planet p = planetList.get(position);
      holder.planetNameView.setText(p.getName());
      holder.distView.setText("" + p.getDescr());
      holder.chk.setChecked(p.getChecked());
  
  
      return v;
  } 
  </code></pre>
  
  <p>}</p>
  
  <p>Now, when I try to run the code on Eclipse, there is an error because the listener is no longer in the MainActivity <strong>chk.setOnCheckedChangeListener((MainActivity) context);</strong> 
  - I had move it to the fragment inner class. </p>
  
  <p>Now, I change the code to <strong>chk.setOnCheckedChangeListener((MainActivity.PlaceholderFragment) context);</strong> and it says I cannot Cast from Context to <code>MainActivity.PlaceholderFragment</code>. </p>
  
  <p>Does anyone know how I can solve this problem?</p>