【发布时间】:2016-01-27 15:11:59
【问题描述】:
我的列表视图项目有这个布局:
<?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>
对于 ImageView 的 onClick 方法:
android:onClick="addToCartClick"
点击 ImageView 但未获得正确视图时,onClick 会正确触发。
我想在 onClick 发生时替换图像,它会替换,但不仅限于此 ImageView。它取代了其他几个。这似乎是随机的。
onClick 的代码如下:
public void addToCartClick(View view) {
ImageView img = (ImageView) view;
img.setImageResource(R.drawable.cart_add);
}
适配器代码:
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;
}
}
我错过了什么?
【问题讨论】:
-
发布您的适配器代码
-
编辑以反映您的要求。
-
问题可能在您的适配器中,您没有实现 ViewHolder 模式,这可能会返回带有错误 ID 的正确视图。请在此处发布您的适配器。
-
试试 - img.setImageResource(null); img.setImageResource(R.drawable.cart_add);
-
不能将 null 设置为 setImageResource。
标签: android android-layout listview onclick