android - onClick fired, but does not get right listview item -


i have layout items of listview:

    <?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_gravity="center_horizontal"     android:padding="10dp">      <imageview         android:id="@+id/carticon"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centervertical="true"         android:layout_marginright="10dp"         android:onclick="addtocartclick"         android:src="@drawable/ic_launcher" />      <textview         android:id="@+id/itemname"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_margin="5dp"         android:layout_torightof="@id/carticon"         android:text="filé mignon"         android:textappearance="@android:style/textappearance.small"         android:typeface="normal" />      <tablelayout         android:id="@+id/tableinfo"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_below="@id/itemname"         android:layout_torightof="@id/carticon"         android:weightsum="3">          <tablerow             android:layout_width="match_parent"             android:layout_height="match_parent">              <textview                 android:id="@+id/qtytext"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_weight="1"                 android:text="qtd"                 android:textalignment="center"                 android:textappearance="?android:attr/textappearancesmall" />              <textview                 android:id="@+id/pricetext"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_weight="1"                 android:text="preço"                 android:textalignment="center"                 android:textappearance="?android:attr/textappearancesmall" />              <textview                 android:id="@+id/totaltext"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_weight="1"                 android:text="total"                 android:textalignment="center"                 android:textappearance="?android:attr/textappearancesmall" />          </tablerow>          <tablerow             android:layout_width="match_parent"             android:layout_height="match_parent">              <textview                 android:id="@+id/qty"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_weight="1"                 android:text="2 kg"                 android:textalignment="center"                 android:textappearance="?android:attr/textappearancesmall" />              <textview                 android:id="@+id/price"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_weight="1"                 android:text="r$ 75,00"                 android:textalignment="center"                 android:textappearance="?android:attr/textappearancesmall" />              <textview                 android:id="@+id/total"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_weight="1"                 android:text="r$ 150,00"                 android:textalignment="center"                 android:textappearance="?android:attr/textappearancesmall" />          </tablerow>     </tablelayout>  </relativelayout> 

and imageview onclick method:

android:onclick="addtocartclick" 

the onclick fires when tap imageview not correct view.
want replace image when onclick occurs, , replaces, not imageview. replaces several others. seems random.
code onclick @ follows:

 public void addtocartclick(view view) {     imageview img = (imageview) view;     img.setimageresource(r.drawable.cart_add); }   

adapter code:

public class shoplistadapter extends arrayadapter<shoplistitem> {  context context; int resource; shoplistitem[] objects;  public shoplistadapter(context context, int resource, shoplistitem[] objects) {     super(context, resource, objects);      this.context = context;     this.resource = resource;     this.objects = objects; }  @override public view getview(int position, view convertview, viewgroup parent) {      if (convertview == null) {         // inflate layout         layoutinflater inflater = ((activity) context).getlayoutinflater();         convertview = inflater.inflate(resource, parent, false);     }      // object item based on position     shoplistitem item = objects[position];      // textview , set text (item name) , tag (item id) values     textview itemname = (textview) convertview.findviewbyid(r.id.itemname);     itemname.settext(item.itemname);      textview qty = (textview) convertview.findviewbyid(r.id.qty);     qty.settext(item.quantity + "kg");      textview price = (textview) convertview.findviewbyid(r.id.price);     price.settext("r$" + item.price);      textview total = (textview) convertview.findviewbyid(r.id.total);     total.settext("r$" + item.total);      return convertview; } 

}

what missing?

take boolean in shoplistitem class -

  public boolean isselected;  

and check in getview method , set image according boolean, when scroll list view image set particular imageview selected previously-

@override public view getview(int position, view convertview, viewgroup parent) {      if (convertview == null) {         // inflate layout         layoutinflater inflater = ((activity) context).getlayoutinflater();         convertview = inflater.inflate(resource, parent, false);     }      // object item based on position     shoplistitem item = objects[position];      // textview , set text (item name) , tag (item id) values     textview itemname = (textview) convertview.findviewbyid(r.id.itemname);     itemname.settext(item.itemname);      textview qty = (textview) convertview.findviewbyid(r.id.qty);     qty.settext(item.quantity + "kg");      textview price = (textview) convertview.findviewbyid(r.id.price);     price.settext("r$" + item.price);      textview total = (textview) convertview.findviewbyid(r.id.total);     total.settext("r$" + item.total);      imageview img = (imageview) view.findviewbyid(r.id.carticon);     img.settag(position);      if ( objects.isselected ){         img.setimageresource(r.drawable.cart_add);     }else{         // can set default image here     }   }      return convertview; } 

and add code-

 public void addtocartclick(view view) {     object.get( integer.parseint(view.gettag()) ).isselected = true;     notifydatasetchanged();  }   

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -