title: unable to implement onclicklistener in inflated layout
body: <p>I am quite new to Android. I am doing a shopping cart app. In my cart page, I display the product image,name,id,quantity,price,total and delete button for each item. </p>
  
  <p>I have used table layout for the above view in item.xml and inflated it in another tablelayout in cart.xml. </p>
  
  <p>The problem is that when i add onclick listener to the remove button, it works only for the first row instead of the corresponding row.</p>
  
  <p>Am i doing it the right way? Please help me. Thanks in advance.</p>
  
  <pre><code>//item.xml
  
  &lt;TableLayout
  android:id="@+id/Item"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:background="#ffffff"
  &gt;  
  &lt;TableRow
  android:id="@+id/tr"
  android:layout_width="fill_parent"
  android:layout_height="97px"
  android:padding="2dip"
  android:orientation="horizontal"
  &gt;
  &lt;LinearLayout
  android:id="@+id/linear1"
  android:layout_width="75px"
  android:layout_height="95px"
  android:orientation="vertical"
  &gt;
  &lt;ImageView
  android:id="@+id/product"
  android:layout_width="60px"
  android:layout_height="60px"
  
  &gt;
  &lt;/ImageView&gt;
  &lt;TextView
  android:id="@+id/pname"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textSize="12sp"
  android:layout_marginLeft="5px"
  android:textColor="#000000"
  &gt;
  &lt;/TextView&gt;
  &lt;TextView
  android:id="@+id/pid"
  android:layout_width="wrap_content"
  android:layout_height="16px"
  android:textSize="12sp"
  android:layout_marginLeft="5px"
  android:textColor="#000000"
  &gt;
  &lt;/TextView&gt;
  &lt;/LinearLayout&gt;
  
  &lt;LinearLayout
  android:id="@+id/linear2"
  android:layout_width="80px"
  android:layout_height="74px"
  android:orientation="vertical"
  &gt;
  &lt;TextView
  android:id="@+id/qty_t"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginBottom="11px"
  android:layout_marginLeft="20px"
  android:text="Qty: "
  android:textSize="12sp"
  android:layout_gravity="right"
  android:textColor="#000000"
  &gt;
  
  &lt;/TextView&gt;
  &lt;TextView
  android:id="@+id/price"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Price: "
  android:textSize="12sp"
  android:layout_gravity="right"
  android:textColor="#000000"
  &gt;
  &lt;/TextView&gt;
  &lt;TextView
  android:id="@+id/itemtot"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Item Total: "
  android:textSize="12sp"
  android:layout_gravity="right"
  android:textColor="#000000"
  &gt;
  &lt;/TextView&gt;
  &lt;/LinearLayout&gt;
  &lt;LinearLayout
  android:id="@+id/linear3"
  android:layout_width="95px"
  android:layout_height="110px"
  android:orientation="vertical"
  &gt;
  &lt;EditText
  android:id="@+id/qty"
  android:numeric="integer" 
  android:layout_width="55px"
  android:layout_height="29px"
   android:textSize="10sp"&gt;
  &lt;/EditText&gt;
  &lt;TextView
  android:id="@+id/price_amt"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  
  android:textSize="12sp"
  android:textColor="#000000"
  &gt;
  &lt;/TextView&gt;
  &lt;TextView
  android:id="@+id/item_amt"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  
  android:textSize="12sp"
  android:textColor="#000000"
  &gt;
  &lt;/TextView&gt;
  &lt;CheckBox
  android:id="@+id/gift_option"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Add Gift Options"
  android:textSize="12sp"
  android:textColor="#000000"
  
  &gt;
  &lt;/CheckBox&gt;
  &lt;/LinearLayout&gt;
  &lt;LinearLayout
  android:id="@+id/linear4"
  android:layout_width="68px"
  android:layout_height="69px"
  android:orientation="vertical"
  android:layout_gravity="right"
  
  &gt;
  &lt;ImageView
  android:id="@+id/remove"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:layout_marginTop="10px"
  android:src="@drawable/x"
  &gt;
  
  &lt;/ImageView&gt;
  &lt;/LinearLayout&gt;
  
  &lt;/TableRow&gt;
  &lt;View
          android:layout_height="3dip"
          android:background="#FF909090" /&gt;
  &lt;/TableLayout&gt;
  
  
  //cart.xml
  &lt;ScrollView
  android:id="@+id/widget129"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  xmlns:android="http://schemas.android.com/apk/res/android"
  &gt;
  &lt;LinearLayout
  android:id="@+id/widget120"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#ffffff"
  android:orientation="vertical"
  xmlns:android="http://schemas.android.com/apk/res/android"
  &gt;
  &lt;TableLayout
  android:id="@+id/cart_item_table"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  &gt;
  &lt;/TableLayout&gt;
  &lt;/LinearLayout&gt;
  &lt;/ScrollView&gt;
  </code></pre>
  
  <p>my java code is :</p>
  
  <pre><code>public class cart3 extends Activity  {
  
      /** Called when the activity is first created. */
      String[] product = new String[]{
              "Ipod 1",
              "ipod 2",
              "ipod 3",
              "ipod 4",
              "ipod 5"
                      };
      String[] pid = new String[]{
              "#1000",
              "#1001",
              "#1002",
              "#1003",
              "#1004"
                      };
     Integer[] pimg = {
              R.drawable.sample_2, R.drawable.sample_1,
              R.drawable.i2, R.drawable.i1,
              R.drawable.i3, R.drawable.i4,
      };
     String[] Qty =  {
             "1","2",
             "1","3",
             "1","2",
      };
     String[] price =  {
             "5000","7500",
             "8000","5500",
             "7200","5000",
      };
  
  
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.cart);
          addCartContent();
  
      }
  **//here is the problem**
      private OnClickListener delimg = new OnClickListener() { 
  
  
          @Override
          public void onClick(View arg0) {
              // TODO Auto-generated method stub
  
              TextView tv=(TextView) findViewById(R.id.pname);
              tv.setText("Deleted Item!");    
  
          } 
      }; 
  
      public void addCartContent()
      {
  
      int m_amt=0;
      View itemView;
      // "cart_item_table" is the id of the item table in cart.xml  
      TableLayout tl = (TableLayout)findViewById(R.id.cart_item_table);
      // we will use service from inflater   
       LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);  
      //doing a loop to add many times the cart xml  
            for(int i = 0; i &lt; product.length; i++) { 
               // "item" is the name of the xml file "item.xml".   
              // Create the itemView who will be added.  
             // itemView = cart.xml  
  
             itemView = inflater.inflate(R.layout.item,null);
             //get the image id and set it to image in array pimg
             ImageView im = (ImageView) itemView.findViewById(R.id.product);
             im.setImageDrawable(getResources().getDrawable(pimg[i]));
            // get the product name, and set the text to element from array  
              TextView t1 = (TextView) itemView.findViewById(R.id.pname);   
              t1.setText(product[i]);  
           // get the product id, and set the text to element from array  
               TextView t2 = (TextView) itemView.findViewById(R.id.pid);   
               t2.setText(pid[i]);  
            // get the product qty, and set the text to element from array  
                EditText t3 = (EditText) itemView.findViewById(R.id.qty);   
                t3.setText(Qty[i]); 
            // get the product price, and set the text to element from array  
                 TextView t4 = (TextView) itemView.findViewById(R.id.price_amt);   
                 t4.setText("Rs."+price[i]+".00");
           // get the item total, and set the text to product of price and qty  
                 TextView t5 = (TextView) itemView.findViewById(R.id.item_amt);
                 int pr = Integer.parseInt(price[i].trim());
                 int quan=Integer.parseInt(Qty[i].trim());
                 int item_amt=pr*quan;
                  String i_amt = Integer.toString(item_amt);
                //Calculate the merchandise total by adding all item_amt
  
                  m_amt=m_amt+item_amt;
                  t5.setText("Rs."+i_amt+".00"); 
                  ImageView x= (ImageView)itemView.findViewById(R.id.remove);
                  x.setOnClickListener(delimg);
  
                  //add the itemView  
                  tl.addView(itemView); 
  
                    }//end of for loop
      }//end of addCartContent()
  </code></pre>