title: Custom Listview contains a button I want to set onclicklistener on the button
body: <p>I made a custom listview with a button and textviews in it.
  What i want to do is -
  On the click of a button,
  I want to get the index of listview.</p>
  
  <p>I used HashMap to store the key value pair.</p>
  
  <p>Here is my Activity code:</p>
  
  <pre><code>package com.hisham.listviewexhisham;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemClickListener;
  import android.widget.Button;
  import android.widget.ListView;
  import android.widget.SimpleAdapter;
  import android.widget.TextView;
  
  public class MainActivity extends Activity {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          ListView lv= (ListView)findViewById(R.id.listview);
          final TextView tvTesting = (TextView)findViewById(R.id.tvTesting);
  
          // create the grid item mapping
          String[] from = new String[] {"quantity", "item", "cost", "remove"};
          int[] to = new int[] { R.id.quantity, R.id.item, R.id.cost, R.id.remove};
          ArrayList&lt;String&gt; productNames = new ArrayList&lt;String&gt;();
          productNames.add("Trumbull 125");
          productNames.add("Trumbull 135");
          productNames.add("Trumbull 17'' Monitor");
          productNames.add("Trumbull Keyboard");
          productNames.add("Trumbull 2TB HD");
  
          // prepare the list of all records
          List&lt;HashMap&lt;String, String&gt;&gt; fillMaps = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
          for(int i = 0; i &lt; productNames.size(); i++){
              HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
              map.put("quantity", "" + i);
              map.put("item", productNames.get(i));
              map.put("cost", "$ 600.99");
              map.put("remove", "X");
              fillMaps.add(map);
          }
  
          // fill in the grid_item layout
          SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid, from, to);
          lv.setAdapter(adapter);
  
  
  
          lv.setOnItemClickListener(new OnItemClickListener() {
  
              public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2,
                      long arg3) {
                  // TODO Auto-generated method stub
  
                      tvTesting.setText("Id is - " +arg2);
  
  
              }
          });
  
  
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.activity_main, menu);
          return true;
      }
      }
  </code></pre>
  
  <p>here is grid.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
  
      &lt;LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal" &gt;
  
          &lt;TextView
              android:id="@+id/quantity"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="1"
              android:width="20dip" /&gt;
  
          &lt;TextView
              android:id="@+id/item"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="1.01"
              android:text="Trumbull"
              android:textAppearance="?android:attr/textAppearanceSmall" /&gt;
  
          &lt;TextView
              android:id="@+id/cost"
              android:layout_width="75dp"
              android:layout_height="wrap_content"
              android:layout_gravity="right"
              android:gravity="right"
              android:text="$ 500.99" /&gt;
  
          &lt;Button
              android:id="@+id/remove"
              style="?android:attr/buttonStyleSmall"
              android:layout_width="30dp"
              android:layout_height="30dp"
              android:text="X" /&gt;
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>and this is my main.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/main"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" &gt;
  
      &lt;TextView
          android:id="@+id/tvTesting"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Large Text"
          android:textAppearance="?android:attr/textAppearanceLarge" /&gt;
  
      &lt;ListView
          android:id="@+id/listview"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" &gt;
      &lt;/ListView&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>