I have this layout for my items of my 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 for ImageView the onClick method:
android:onClick="addToCartClick"
The onClick fires properly when tap the ImageView but does not get the correct view.
I want to replace the image when onClick occurs, and it replaces, but not only to this ImageView. It replaces for several others. It seems random.
The code for onClick is at 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 the layout
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(resource, parent, false);
}
// object item based on the position
ShopListItem item = objects[position];
// get the TextView and then set the text (item name) and 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 am I missing?