android - unable to implement onclicklistener in inflated layout -


i quite new android. doing shopping cart app. in cart page, display product image,name,id,quantity,price,total , delete button each item.

i have used table layout above view in item.xml , inflated in tablelayout in cart.xml.

the problem when add onclick listener remove button, works first row instead of corresponding row.

am doing right way? please me. in advance.

//item.xml  <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" >   <tablerow android:id="@+id/tr" android:layout_width="fill_parent" android:layout_height="97px" android:padding="2dip" android:orientation="horizontal" > <linearlayout android:id="@+id/linear1" android:layout_width="75px" android:layout_height="95px" android:orientation="vertical" > <imageview android:id="@+id/product" android:layout_width="60px" android:layout_height="60px"  > </imageview> <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" > </textview> <textview android:id="@+id/pid" android:layout_width="wrap_content" android:layout_height="16px" android:textsize="12sp" android:layout_marginleft="5px" android:textcolor="#000000" > </textview> </linearlayout>  <linearlayout android:id="@+id/linear2" android:layout_width="80px" android:layout_height="74px" android:orientation="vertical" > <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" >  </textview> <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" > </textview> <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" > </textview> </linearlayout> <linearlayout android:id="@+id/linear3" android:layout_width="95px" android:layout_height="110px" android:orientation="vertical" > <edittext android:id="@+id/qty" android:numeric="integer"  android:layout_width="55px" android:layout_height="29px"  android:textsize="10sp"> </edittext> <textview android:id="@+id/price_amt" android:layout_width="wrap_content" android:layout_height="wrap_content"  android:textsize="12sp" android:textcolor="#000000" > </textview> <textview android:id="@+id/item_amt" android:layout_width="wrap_content" android:layout_height="wrap_content"  android:textsize="12sp" android:textcolor="#000000" > </textview> <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"  > </checkbox> </linearlayout> <linearlayout android:id="@+id/linear4" android:layout_width="68px" android:layout_height="69px" android:orientation="vertical" android:layout_gravity="right"  > <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" >  </imageview> </linearlayout>  </tablerow> <view         android:layout_height="3dip"         android:background="#ff909090" /> </tablelayout>   //cart.xml <scrollview android:id="@+id/widget129" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" > <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" > <tablelayout android:id="@+id/cart_item_table" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > </tablelayout> </linearlayout> </scrollview> 

my java code :

public class cart3 extends activity  {      /** called when activity 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 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" id of item table in cart.xml       tablelayout tl = (tablelayout)findviewbyid(r.id.cart_item_table);     // use service inflater         layoutinflater inflater = (layoutinflater)getsystemservice(layout_inflater_service);       //doing loop add many times cart xml             for(int = 0; < product.length; i++) {               // "item" name of xml file "item.xml".                // create itemview added.              // itemview = cart.xml               itemview = inflater.inflate(r.layout.item,null);            //get image id , set image in array pimg            imageview im = (imageview) itemview.findviewbyid(r.id.product);            im.setimagedrawable(getresources().getdrawable(pimg[i]));           // product name, , set text element array               textview t1 = (textview) itemview.findviewbyid(r.id.pname);                t1.settext(product[i]);            // product id, , set text element array                textview t2 = (textview) itemview.findviewbyid(r.id.pid);                 t2.settext(pid[i]);             // product qty, , set text element array                 edittext t3 = (edittext) itemview.findviewbyid(r.id.qty);                  t3.settext(qty[i]);            // product price, , set text element array                  textview t4 = (textview) itemview.findviewbyid(r.id.price_amt);                   t4.settext("rs."+price[i]+".00");          // item total, , set text product of price , 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 merchandise total adding 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 itemview                   tl.addview(itemview);                     }//end of loop     }//end of addcartcontent() 

hey think code working fine, in code instead of using

public void onclick(view arg0) {             // todo auto-generated method stub              textview tv=(textview) findviewbyid(r.id.pname);             tv.settext("deleted item!");              } 

use

 public void onclick(view view) {             // todo auto-generated method stub              textview tv=(textview) view.findviewbyid(r.id.pname);             tv.settext("deleted item!");              } 

in instance since have not specified view context first view matches id, in case first row.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -