gpt4 book ai didi

android - 列表中的数据在滚动时重复

转载 作者:行者123 更新时间:2023-11-30 01:54:25 25 4
gpt4 key购买 nike

虽然我的点击工作正常,但我的 ListView 的数据在滚动时重复出现,我知道这是关于 setTag 或定位的问题,但我无法修复它......所以我需要你们的帮助

这是我的适配器的 getView 函数:-

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
final int i = position;
final WholeProductList listItem = mProductList.get(position);

if (convertView == null) {
convertView = mLayoutInflater.inflate(
R.layout.list_comapare_product, parent, false);
viewHolder = new ViewHolder();
viewHolder.tvProductName = (TextView) convertView
.findViewById(R.id.tv_product_name);
viewHolder.tvProductName.setText(mProductList.get(position)
.getName());
viewHolder.imgProduct = (ImageView) convertView
.findViewById(R.id.img_product);
viewHolder.imgadd = (ImageView) convertView
.findViewById(R.id.img_plus);
viewHolder.mRlConatiner = (RelativeLayout) convertView
.findViewById(R.id.rl_container);
convertView.setTag(viewHolder);

} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.mRlConatiner.setTag(position);
viewHolder.mRlConatiner.setOnClickListener(new OnClickListener() {
@SuppressWarnings("static-access")
@Override
public void onClick(View v) {
if (tickedItems.contains((Integer) v.getTag())) {
// Already ticked, set to plus
viewHolder.imgadd.setImageDrawable(activity.getResources()
.getDrawable(R.drawable.plus));
tickedItems.remove((Integer) v.getTag());
if (((AppController) activity.getApplicationContext()).cartItemList
.contains(listItem)) {
((AppController) activity.getApplicationContext()).cartItemList
.remove(listItem);
}
} else {
tickedItems.add((Integer) v.getTag());
viewHolder.imgadd.setImageDrawable(activity.getResources()
.getDrawable(R.drawable.tick));
if (!((AppController) activity.getApplicationContext()).cartItemList
.contains(listItem)) {
((AppController) activity.getApplicationContext()).cartItemList
.add(listItem);
}
}
Session.getmCountImage().setvisibilityOn();
}
});
if (tickedItems.contains(position))
viewHolder.imgadd.setImageDrawable(activity.getResources()
.getDrawable(R.drawable.tick));

else
viewHolder.imgadd.setImageDrawable(activity.getResources()
.getDrawable(R.drawable.plus));

return convertView;
}

谢谢

最佳答案

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
final int i = position;
final WholeProductList listItem = mProductList.get(position);

if (convertView == null) {
convertView = mLayoutInflater.inflate(
R.layout.list_comapare_product, parent, false);
viewHolder = new ViewHolder();
viewHolder.tvProductName = (TextView) convertView
.findViewById(R.id.tv_product_name);
viewHolder.imgProduct = (ImageView) convertView
.findViewById(R.id.img_product);
viewHolder.imgadd = (ImageView) convertView
.findViewById(R.id.img_plus);
viewHolder.mRlConatiner = (RelativeLayout) convertView
.findViewById(R.id.rl_container);
convertView.setTag(viewHolder);

} else {
viewHolder = (ViewHolder) convertView.getTag();
}

/////////////This line should be here///////////////////////////////////
viewHolder.tvProductName.setText(mProductList.get(position)
.getName());
/////////////////////////////////////////////////////////////////////////
viewHolder.mRlConatiner.setTag(position);
viewHolder.mRlConatiner.setOnClickListener(new OnClickListener() {
@SuppressWarnings("static-access")

关于android - 列表中的数据在滚动时重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32499326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com