9

我正在使用 baseadapter 创建一个自定义列表视图。我的列表中有 10 个列表项。我的问题是之后 6 个项目,前 4 个是重复的。我只是在 getview 中打印了位置值。它给出了 0、1、2、3 ,4,5,6,7,8,9,0,1,2,3。我的代码如下。

提前谢谢

public class ProductListAdapter extends BaseAdapter  implements OnClickListener{    

/*
 * developer :sanu
 * date :10-4-2013
 * time :3.34 pm
 */
public View row;
private String[] productName;
private String[] producttype;
private String[] priceRangeFrom;
private String[] priceRangeTo;
private String[] productImage;
private Activity activity;
private static LayoutInflater inflater=null;
static String posClicked;
ViewHolder holder;
Integer height1;
Integer width1;
Typeface tf;
Integer FirmaCount;
public ImageLoader imageLoader; 
public ProductListAdapter(Activity a,String[] name,String[] type,String[] price_from,String[] price_to,String[] image,Typeface tf) {
    activity    =  a;
    productName = name;
    producttype = type;
    priceRangeFrom = price_from;
    priceRangeTo = price_to;
    productImage = image;       
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
    imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
    return productName.length;
}
public Object getItem(int position) {
    return position;
}
public long getItemId(int position) {
    return position;
} 
public int getViewTypeCount (int position)
{
    return position;
}
public static class ViewHolder{
    public TextView nameProduct;
    public TextView typeProduct;
    public TextView priceRangeProduct;
    public ImageView productImage;
    public ImageView plusImage;
    public RelativeLayout mainLayout;
    public int position;  
}
public View getView(int position, View convertView, ViewGroup parent) {     
    if(convertView == null){            
        convertView = inflater.inflate(R.layout.product_list_details,parent, false);
        holder=new ViewHolder();
        holder.nameProduct =(TextView)convertView.findViewById(R.id.name);
        holder.typeProduct =(TextView)convertView.findViewById(R.id.product);
        holder.priceRangeProduct =(TextView)convertView.findViewById(R.id.pricerange);
        holder.productImage =(ImageView)convertView.findViewById(R.id.image);
        holder.plusImage =(ImageView)convertView.findViewById(R.id.dot);
        holder.mainLayout = (RelativeLayout)convertView.findViewById(R.id.mainlayout);
        holder.nameProduct.setText(productName[position]);      
        if(producttype[position].length()>18)
        {
            holder.typeProduct.setText(producttype[position].substring(0,18)+"...");
        }
        else
        {
            holder.typeProduct.setText(producttype[position]);
        }
        holder.priceRangeProduct.setText(priceRangeFrom[position].substring(0,priceRangeFrom[position].length()-2)+" To "+priceRangeTo[position].substring(0, priceRangeTo[position].length()-2));          
        imageLoader.DisplayImage(productImage[position], holder.productImage);
        convertView.setTag(holder);                     
    }
    else
    {           
        holder = (ViewHolder)convertView.getTag();

    }       
    holder.plusImage.setTag(Integer.toString(position));
    holder.plusImage.setOnClickListener(this);  
    holder.mainLayout.setTag(Integer.toString(position));
    holder.mainLayout.setOnClickListener(this); 
    return convertView;
} 
4

4 回答 4

6

这听起来像是 View re-cycling 的情况。Android 会将预先填充的视图传递给 getView 方法。这样做是为了尽量减少对象创建。当现有的行视图滚动到屏幕外时,Android 可能会尝试回收该视图以显示现在在屏幕上的行。您需要考虑这样一个事实,即该视图可能已用于显示另一行的数据(现在不在屏幕上)。

您有以下行

holder.typeProduct.setText

在以下条件内:

if(convertView == null){            

将那条线移到条件之外,一切都应该很好。

于 2013-11-09T06:52:47.227 回答
3

就像 EJK 说的那样。您没有正确回收视图。将您的代码更改为此并注意我将 setText 调用放在哪里

public View getView(int position, View convertView, ViewGroup parent) {     
if(convertView == null){            
    convertView = inflater.inflate(R.layout.product_list_details,parent, false);
    holder=new ViewHolder();
    holder.nameProduct =(TextView)convertView.findViewById(R.id.name);
    holder.typeProduct =(TextView)convertView.findViewById(R.id.product);
    holder.priceRangeProduct =(TextView)convertView.findViewById(R.id.pricerange);
    holder.productImage =(ImageView)convertView.findViewById(R.id.image);
    holder.plusImage =(ImageView)convertView.findViewById(R.id.dot);
    holder.mainLayout = (RelativeLayout)convertView.findViewById(R.id.mainlayout);

    convertView.setTag(holder);                     
}
else
{           
    holder = (ViewHolder)convertView.getTag();

}       
    holder.plusImage.setTag(Integer.toString(position));
    holder.plusImage.setOnClickListener(this);  
    holder.mainLayout.setTag(Integer.toString(position));
    holder.mainLayout.setOnClickListener(this); 

    //setText functions are here
    holder.nameProduct.setText(productName[position]); 


    if(producttype[position].length()>18)
    {
        holder.typeProduct.setText(producttype[position].substring(0,18)+"...");
    }
    else
    {
        holder.typeProduct.setText(producttype[position]);
    }
    holder.priceRangeProduct.setText(priceRangeFrom[position].substring(0,priceRangeFrom[position].length()-2)+" To "+priceRangeTo[position].substring(0, priceRangeTo[position].length()-2));

    imageLoader.DisplayImage(productImage[position], holder.productImage);

    return convertView;
} 
于 2013-11-09T10:01:27.843 回答
1

改变你getView

public View getView(int position, View convertView, ViewGroup parent) {     
    if(convertView == null){            
        convertView = inflater.inflate(R.layout.product_list_details,parent, false);
        holder=new ViewHolder();
        holder.nameProduct =(TextView)convertView.findViewById(R.id.name);
        holder.typeProduct =(TextView)convertView.findViewById(R.id.product);
        holder.priceRangeProduct =(TextView)convertView.findViewById(R.id.pricerange);
        holder.productImage =(ImageView)convertView.findViewById(R.id.image);
        holder.plusImage =(ImageView)convertView.findViewById(R.id.dot);
        holder.mainLayout = (RelativeLayout)convertView.findViewById(R.id.mainlayout);
        convertView.setTag(holder); 
        } else { 
         holder = (ViewHolder) convertView.getTag();
        } 
        holder.nameProduct.setText(productName[position]);      
        if(producttype[position].length()>18)
        {
            holder.typeProduct.setText(producttype[position].substring(0,18)+"...");
        }
        else
        {
            holder.typeProduct.setText(producttype[position]);
        }
        holder.priceRangeProduct.setText(priceRangeFrom[position].substring(0,priceRangeFrom[position].length()-2)+" To "+priceRangeTo[position].substring(0, priceRangeTo[position].length()-2));          
        imageLoader.DisplayImage(productImage[position], holder.productImage);

       holder.plusImage.setTag(Integer.toString(position));
       holder.plusImage.setOnClickListener(this);  
       holder.mainLayout.setTag(Integer.toString(position));
       holder.mainLayout.setOnClickListener(this); 
       return convertView;
} 

还要检查这个

ListView 的回收机制是如何工作的

于 2013-11-09T06:53:41.613 回答
1

更改 getView()

ViewHolder之前声明if (convertView == null)

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.product_list_details,
                    parent, false);
            holder = new ViewHolder();
            holder.nameProduct = (TextView) convertView.findViewById(R.id.name);
            holder.typeProduct = (TextView) convertView
                    .findViewById(R.id.product);
            holder.priceRangeProduct = (TextView) convertView
                    .findViewById(R.id.pricerange);
            holder.productImage = (ImageView) convertView
                    .findViewById(R.id.image);
            holder.plusImage = (ImageView) convertView.findViewById(R.id.dot);
            holder.mainLayout = (RelativeLayout) convertView
                    .findViewById(R.id.mainlayout);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();

        }
        holder.nameProduct.setText(productName[position]);
        if (producttype[position].length() > 18) {
            holder.typeProduct.setText(producttype[position].substring(0, 18)
                    + "...");
        } else {
            holder.typeProduct.setText(producttype[position]);
        }
        holder.priceRangeProduct.setText(priceRangeFrom[position].substring(0,
                priceRangeFrom[position].length() - 2)
                + " To "
                + priceRangeTo[position].substring(0,
                        priceRangeTo[position].length() - 2));
        imageLoader.DisplayImage(productImage[position], holder.productImage);
        holder.plusImage.setTag(Integer.toString(position));
        holder.plusImage.setOnClickListener(this);
        holder.mainLayout.setTag(Integer.toString(position));
        holder.mainLayout.setOnClickListener(this);
        return convertView;
    }
于 2013-11-09T07:07:03.990 回答