将项添加到适配器会中断onItemClickListener事件

时间:2011-11-10 22:18:40

标签: java android android-layout

重新组织自定义ListView和Adapter以便它利用ViewHolder类之后,代码最初似乎起作用。但是,当向下滚动到底部并向列表中添加更多项时,列表元素突然​​失去了单击的能力。在使用ViewHolder之前不是这种情况。列表元素中的自定义对象正常运行。

适配器getView:

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if(convertView == null)
    {
        convertView = View.inflate(this.context, R.layout.company_listing, null);
        Log.d(Cloud.DEBUG_TAG, "Inflating View...");    
        holder = new ViewHolder(convertView, context);

        convertView.setTag(holder);

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

    holder.populateFrom(companies.get(position),logoDown);

    return convertView;
}

ViewHolder:

class ViewHolder{

ImageView theLogo;
TextView textName;
ImageView webButton;
ImageView phoneButton;
ImageView favoriteButton;
Context context;
Company currentCompany;



public ViewHolder(View row, Context context){
    theLogo = (ImageView) row.findViewById(R.id.imageLogo);
    textName = (TextView) row.findViewById(R.id.textName);
    webButton = (ImageView) row.findViewById(R.id.imageWeb);
    phoneButton = (ImageView) row.findViewById(R.id.imagePhone);
    favoriteButton = (ImageView) row.findViewById(R.id.imageStar);

    theLogo.setImageResource(R.drawable.no_pic);


    this.context = context;


}

public void setIcon(Bitmap icon)
{

    try {
        if(icon == null) {
            theLogo.setImageResource(R.drawable.no_pic);
        } else {
            theLogo.setImageBitmap(icon);
        }
    } catch(Exception e) {}

}

public void populateFrom(Company oneCompany,CachedLogoDownloader logos){

    currentCompany = oneCompany;

    Bitmap tIcon = logos.getLogo(this);

    try {
        if(tIcon == null) {
            theLogo.setImageResource(R.drawable.no_pic);
        } else {
            theLogo.setImageBitmap(tIcon);
        }
    } catch(Exception e) {}


    //create listener for favorite star icon thingy
    favoriteButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

        //...code removed...
        }
    });

    //set company name
    textName.setText(currentCompany.getName());

    //resolve web button visibility
    if(!currentCompany.getWeb().equals("")) {
        webButton.setVisibility(View.VISIBLE);
        webButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                //...code removed...
            }
        });
    }
    else {
        webButton.setVisibility(View.GONE);
    }

    //resolve phone button visibility
    if(!currentCompany.getPhone().equals("")) {
        phoneButton.setVisibility(View.VISIBLE);
        phoneButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                //...code removed...
            }
        });
    }
    else {
        phoneButton.setVisibility(View.GONE);
    }


}}}

1 个答案:

答案 0 :(得分:0)

我知道问题出在哪里:
仅当convertView == null时才实例化ViewHolder。所以调用findViewById指的是屏幕外的ListViewItems。如果您将构造函数代码放入populateFrom,一切都应该没问题