基础适配器无法正常工作

时间:2014-09-14 10:42:11

标签: android baseadapter

您好我正在使用BaseAdapter,但总是显示我的最后一项。

public View getView(int position, View convertView, ViewGroup parent){
    SearchViewHolder orderViewHolder = null;
    int type = getItemViewType(position);

    if (convertView == null) {
        orderViewHolder = new SearchViewHolder();

        convertView = inflater.inflate(R.layout.order_list_row, null);
        orderViewHolder.setproductname((TextView) convertView
                .findViewById(R.id.orderTitle));        
        }
        convertView.setTag(orderViewHolder);
    } else {
        orderViewHolder = (SearchViewHolder)convertView.getTag();
    }
    for (int i = 0 ; i<msearchproductname.size();i++) {
        System.out.println("@@ value of productname !!!!:"+msearchproductname.get(i));
        orderViewHolder.getproductname().setText(""+msearchproductname.get(i));  
    }

    return convertView;}

连输出

System.out.println("@@ value of productname !!!!:"+msearchproductname.get(i));

与我们想要的相同。

这里发生了什么错误。在textView中,它始终显示最后一项。请帮帮我。谢谢所有人。

1 个答案:

答案 0 :(得分:1)

好的,你需要删除循环并以这种方式执行

public View getView(int position, View convertView, ViewGroup parent){
    SearchViewHolder orderViewHolder = null;
    int type = getItemViewType(position);
    if (convertView == null) {
        orderViewHolder = new SearchViewHolder();
        convertView = inflater.inflate(R.layout.order_list_row, null);
        orderViewHolder.setproductname((TextView)convertView
            .findViewById(R.id.orderTitle));
        convertView.setTag(orderViewHolder);
    } else {
        orderViewHolder = (SearchViewHolder)convertView.getTag();
    } 

    orderViewHolder.getproductname().setText(""+msearchproductname.get(position));  
    return convertView;
}