title: How do I get the position of a togglebutton that is clicked in a gridview?
body: <p>How do I get the position of the android togglebutton that was clicked in a gridview?</p>
  
  <p>If I use the following example code I dont get a Toast message of the position.</p>
  
  <pre><code>public class HelloGridView extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      GridView gridview = (GridView) findViewById(R.id.gridview);
      gridview.setAdapter(new ToggleButtonAdapter(this));
  
      gridview.setOnItemClickListener(new OnItemClickListener() {
  
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View v, int position,
                  long id) {
              Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
          }
  
      });
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And the toggle adapter:</p>
  
  <pre><code>public class ToggleButtonAdapter extends BaseAdapter {
  private Context mContext;
  
  public ToggleButtonAdapter(Context c) {
      mContext = c;
  }
  
  @Override
  public int getCount() {
      return 5;
  }
  
  @Override
  public Object getItem(int position) {
      return null;
  }
  
  @Override
  public long getItemId(int position) {
      return 0;
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      View view;
      LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
      if (convertView == null) {  // reuse it if it already exists
          view = (View) inflater.inflate(R.layout.items, null);
  
      } else {
          view = (View) convertView;
      }
  
      ToggleButton toggleButton = (ToggleButton) view.findViewById(R.id.toggleButton1);
      TextView textView = (TextView) view.findViewById(R.id.textView1);
      textView.setText("Toggle Button " + position);
  
      /*toggleButton.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              ToggleButton t = (ToggleButton) v.findViewById(R.id.toggleButton1);
  
              Toast.makeText(mContext, Boolean.toString(t.isChecked()), Toast.LENGTH_SHORT).show();
          }
      });*/
      return view;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Uncomment the toggle listener to play with that part and show me how to get the position. Is there a way to inject some data into the toggle button as the grid view is being constructed that I could later reference? I couldn't find a way reading the ToggleButton javadoc.</p>
  
  <p>Thanks much!</p>
  
  <p>EDIT:</p>
  
  <p>Oops! here are the layouts:</p>
  
  <p>main.xml</p>
  
  <pre><code>&lt;GridView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/gridview"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:columnWidth="90dp"
      android:numColumns="auto_fit"
      android:verticalSpacing="10dp"
      android:horizontalSpacing="10dp"
      android:stretchMode="columnWidth"
      android:gravity="center"
      &gt;
  
  
  
  &lt;/GridView&gt;
  </code></pre>
  
  <p>items.xml:</p>
  
  <pre><code>&lt;LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"
      android:orientation="horizontal"&gt;
      &lt;LinearLayout 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent"
          android:orientation="vertical"&gt;
  
          &lt;TextView 
              android:text="Some label" 
              android:id="@+id/textView1" 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content"
              android:layout_margin="10dp"/&gt;
  
          &lt;ToggleButton 
              android:id="@+id/toggleButton1" 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content"
              android:layout_margin="10dp"/&gt;
      &lt;/LinearLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>