0

我有一个 ListView,在其中显示一些产品。我使用扩展 BaseAdapter 类的对象来填充 ListView,更准确地使用 getView(..) 方法。我在每个 itemView 上都有一个 TextView“链接”,如果用户点击它将转到网页。在我的基本适配器中,只有当我的产品包含链接时,我才会在 TextView 上设置一个侦听器。我已经在我的 getView(..) 方法中完成了调试,一切正常,但是在它退出 getView 方法后,如果有一个项目没有链接,它将从另一个项目中获取链接/侦听器列表视图。

适配器类:

public class MatchListBaseAdapter extends BaseAdapter {

private static ArrayList<Match> matchesArrayList;
private LayoutInflater l_Inflater;
private OnClickListener onClickListener;


public MatchListBaseAdapter(Context context, View.OnClickListener listener, ArrayList<Match> results,Activity a) {
    matchesArrayList = results;
    onClickListener = listener;
    l_Inflater = LayoutInflater.from(context);
}

public int getCount() {
    return matchesArrayList.size();
}

public Object getItem(int position) {
    return matchesArrayList.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = l_Inflater.inflate(R.layout.itemlist_match, null);
        holder = new ViewHolder();

        holder.name = (TextView) convertView.findViewById(R.id.oferNameMLI2);
        holder.expireDate = (TextView) convertView.findViewById(R.id.expireDateMLI);
        holder.price = (TextView) convertView.findViewById(R.id.priceMLI);
        holder.companyName = (TextView) convertView.findViewById(R.id.compNameMLI);
        holder.productImage = (ImageView) convertView.findViewById(R.id.productImageMLI);
        holder.companyImage = (ImageView) convertView.findViewById(R.id.companyImageMLI);
        holder.description = (TextView) convertView.findViewById(R.id.moreDetailsMLI);
        holder.digitalySigned = (ImageView) convertView.findViewById(R.id.digitalSignatureImageView);

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

        //populating the holder.. doesn`t have any relevance..

    if(matchesArrayList.get(position).getCompanyLink() != null){
        holder.companyImage.setOnClickListener(onClickListener);
        holder.companyImage.setTag(position);
    }

    return convertView;
}

static class ViewHolder {

    TextView name;
    TextView expireDate;
    TextView price;
    TextView companyName;
    TextView description;
    ImageView productImage;
    ImageView companyImage;
    ImageView digitalySigned;
}

}

创建活动:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview_layout);

    matches = DataManager.getInstance().getListSubscription().get(DataManager.getInstance().getSubscriptionPosition()).getMatchesList();

    ListView lv1 = (ListView) findViewById(R.id.listView_layout);
    DataManager.getInstance().setAdapterMatch(new MatchListBaseAdapter(this, this, matches,this));
    lv1.setAdapter(DataManager.getInstance().getAdapterMatch());

}

只想再次提一下,我已经在 getView(..) 方法中进行了调试,没关系,流程是正确的,但是之后the items in the listView that doesn't supposed to have a listener on the TextView it had from the other items.

Also this happens always for the first item in the listView .. and it is populated with the link from the last item in the listView that contains a link.

我已经为此问题进行了很多搜索,但没有找到任何相关内容,但我认为我的 convertView 有问题,但我无法弄清楚..

多谢

4

1 回答 1

0

问题是,当您重用视图而不为其设置显式onClickListener时,它仍然包含来自另一个产品的旧侦听器 - 来自被重用的视图。尝试进行如下更改:

if(matchesArrayList.get(position).getCompanyLink() != null){
    holder.companyImage.setOnClickListener(onClickListener);
    holder.companyImage.setTag(position);
}
else {
    holder.companyImage.setOnClickListener(null);
}
于 2013-04-11T14:52:25.327 回答