开发者

How to avoid recycled item issue with ListAdapters?

I believe to be following the standard 开发者_开发知识库pattern for avoiding recycled listview data,yet, I fail. As usual, the last two items of the listview (which are below the fold...get data from the other previous positions)

public static class ViewHolder {
    public ImageView img1;
    public ImageView img2;  
    public TextView text1;
    public TextView text2;
    public TextView text3;
    public TextView text4;
  }

public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder holder;
        if( convertView == null ){
            vi = inflater.inflate(R.layout.item, null);
            holder=new ViewHolder();
            holder.text1=(TextView)vi.findViewById(R.id.text1);
            holder.text2=(TextView)vi.findViewById(R.id.text2);
            holder.text3=(TextView)vi.findViewById(R.id.text3);
            holder.text4=(TextView)vi.findViewById(R.id.text4);         
            holder.img1=(ImageView)vi.findViewById(R.id.img1);
                    holder.img2=(ImageView)vi.findViewById(R.id.img2);
            vi.setTag(holder);
        } else {
            holder=(ViewHolder)vi.getTag();
        }

            String shorten=shortenText(t4);
    holder.text.setTag(shorten);
    holder.text.setText(shorten);   

    if(t1){
        holder.text1.setTag(thisMsg.get(D_ENUM.T1));
        holder.innerName.setText(thisMsg.get(thisMsg.get(D_ENUM.T1));
    }
    if(t2){
        holder.text1.setTag(thisMsg.get(D_ENUM.T2));
        holder.innerName.setText(thisMsg.get(thisMsg.get(D_ENUM.T2));
    }
    if(thisMsg.containsKey(D_ENUM.T3)){
        String t3 = shortenText(thisMsg.get(D_ENUM.T3));
        holder.innerDesc.setTag(t3);
        holder.innerDesc.setText(t3);
    }

    holder.img1.setTag(img1);
    imageLoader.DisplayImage(img1, act, holder.img1);

    if(img2 != null) { 
        holder.innerImage.setTag(img2);
        holder.innerImage.setVisibility(View.VISIBLE);
        imageLoader.DisplayImage(img2, act, holder.img2);
    } else {
        holder.innerImage.setTag(img2);
        holder.innerImage.setVisibility(View.GONE);
    }

    return vi;


You have to set the content for all the members of your ViewHolder, even if that means setting them to an empty value if you don't have anything to display.

Understand that the View you get passed (convertView) and the associated ViewHolder (holder) have been populated by previous execution of the getView function; they are not newly created objects with default values

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜