android - onClick 已触发,但未获得正确的 ListView 项

标签 android android-layout listview onclick

我的 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>

对于 ImageView 的 onClick 方法:

android:onClick="addToCartClick"

当点击 ImageView 但没有获得正确的 View 时,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;
}

我错过了什么?

最佳答案

在 ShopListItem 类中取一个 bool 值 -

  public boolean isSelected; 

然后检查 getView 方法并根据此 bool 值设置其图像,因此当您滚动 ListView 时,图像将设置为您之前选择的特定 ImageView -

@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);

    ImageView img = (ImageView) view.findViewById(R.id.cartIcon);
    img.setTag(position);

    if ( objects.isSelected ){

       img.setImageResource(R.drawable.cart_add);
    }else{
        // you can set default image here
    }
  }

    return convertView;
}

并添加此代码-

 public void addToCartClick(View view) {
    object.get( Integer.parseInt(view.getTag()) ).isSelected = true;
    notifyDatasetChanged();
 }  

关于android - onClick 已触发,但未获得正确的 ListView 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35041042/

相关文章:

android - 布局编辑器不再工作 : Rendering Failed with a known bug

c# - 使用 MVVM 在 UWP 中处理 ListView ItemClick 的最佳实践

android - 在 XML 可绘制对象中使用主题引用需要 API 级别 21

Android - 在寻呼机中获取 fragment 的上下文

android - android ics 中的 onItemClick 卡住

android - 在 <API 21 中的颜色选择器中使用主题属性

android - 在 ListView 中查找按钮的位置

java - 将 onClickListener 设置为自定义适配器

android - 延迟加载列表 - 这里需要一点帮助

java - Android 版本 - 4.0.4 中的通话记录与短信日志位于同一位置