本文介绍了ListView View Holder 多次返回重复的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码,
查看持有人类别:
private class ViewHolder {
TextView tv1;
TextView tv2;
TextView tv3;
TextView tv4;
TextView tv5;
TextView tv6;
TextView tv7;
TextView tv8;
TextView tv9;
TextView tv10;
TextView tv11;
TextView tv12;
TextView tv13;
TextView tv14;
EditText edit_qty;
Button btnbonus;
Button btnremove;
}
ListView 自定义适配器 getView() :
ListView Custom Adapters getView() :
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
ViewHolder holder = null;
View view = convertView;
if(view == null){
view = lInflater.inflate(R.layout.add_productqty_listitem, parent,
false);
Typeface font = Typeface.createFromAsset(ctx.getAssets(),
"gothic.ttf");
holder = new ViewHolder();
final ProductInfo product = data.get(position);
ctx.mProductIdList.add(product.getProductId());
try {
holder.tv1 = (TextView) view
.findViewById(R.id.add_product_label);
holder.tv1.setText(product.getProductName());
holder.tv1.setTypeface(font);
holder.tv1.setVisibility(View.VISIBLE);
holder.tv2 = (TextView) view
.findViewById(R.id.Product_Code_label);
holder.tv2.setTypeface(font);
holder.tv2.setVisibility(View.VISIBLE);
...............
...............
holder.edit_qty.setVisibility(View.VISIBLE);
view.setTag(holder);
} catch (Exception e) {
e.printStackTrace();
}
}else{
holder = (ViewHolder) view.getTag();
}
return view;
}
我不知道viewholder的实现是否正确,ListView有n个之前填充的重复项,你的帮助将不胜感激
I dont know whether the implement of view holder is correct or not , ListView has n number of duplicates which has been populated earlier , your help will be thankfull
推荐答案
一旦您从 getTag 或第一行初始化中获得了 holder 对象,就设置这些值.所以它应该在 if.. else 条件检查之后在 if.. else
语句
Set the values once you get the holder object from getTag or first row initlization. so it should after if.. else condition checkingCheck the comment also i hve mentioned in getview Method after if.. else
statment
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
ViewHolder holder = null;
View view = convertView;
if(view == null){
view = lInflater.inflate(R.layout.add_productqty_listitem, parent,
false);
holder = new ViewHolder();
try{ Typeface font = Typeface.createFromAsset(ctx.getAssets(),
"gothic.ttf");
holder.tv1 = (TextView) view
.findViewById(R.id.add_product_label);
holder.tv1.setTypeface(font);
holder.tv2 = (TextView) view
.findViewById(R.id.Product_Code_label);
holder.tv2.setTypeface(font);
view.setTag(holder);
} catch (Exception e) {
e.printStackTrace();
}
}else{
holder = (ViewHolder) view.getTag();
}
final ProductInfo product = data.get(position);
ctx.mProductIdList.add(product.getProductId());
// set data , text and visibility option after getting holder from view's getTag()
holder.tv1.setText(product.getProductName());
holder.tv1.setVisibility(View.VISIBLE);
holder.tv2.setVisibility(View.VISIBLE);
...............
...............
holder.edit_qty.setVisibility(View.VISIBLE);
return view;
这篇关于ListView View Holder 多次返回重复的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!