制作自定义适配器以实现网格视图时出错

时间:2014-11-29 11:51:35

标签: java android

这是我的自定义适配器的“getView”代码....问题是假设我有一个显示的7个名字的列表....我必须在一行中显示2个联系人作为网格视图....现在为此我将原始列表分成两个列表....现在一个列表有4个联系人,另一个有3个......问题是我的列表在第二个空格中重复一个联系人列表....它不应该是可见的,但它在我向上和向下滚动时不断重复随机联系

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

        viewholder rowView = new viewholder();
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        if(convertView == null)
        {   

        convertView = inflater.inflate(R.layout.d_list, parent, false);
        rowView.tv1 = (TextView) convertView.findViewById(R.id.textView1); 
        rowView.imgv1 = (ImageView) convertView.findViewById(R.id.imageView1);
        rowView.tv2 = (TextView) convertView.findViewById(R.id.textView2);
        rowView.imgv2 = (ImageView) convertView.findViewById(R.id.imageView2);
        rowView.imbg = (ImageView) convertView.findViewById(R.id.imageView3);
        convertView.setTag(rowView);            

        }
        else
        {
            rowView = (viewholder) convertView.getTag();
        }


        rowView.imbg.setImageResource(resid[colors.get(position)]);

        byte[] getin = convertBtob(images.get(position));
        Bitmap bitmap = BitmapFactory.decodeByteArray(getin, 0, getin.length);
        final String a = values.get(position);
        rowView.tv1.setText(a);
        rowView.imgv1.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 75, 75, false));


        if(list1.get(list1.size()-1).equals(a));
        else    
        {
             opt op2 = new opt(values2.get(position));  
             byte[] getin2 = convertBtob(images2.get(position));
             Bitmap bitmap1 = BitmapFactory.decodeByteArray(getin2, 0, getin2.length);
             rowView.tv2.setText(String.valueOf(a));
             rowView.imgv2.setImageBitmap(Bitmap.createScaledBitmap(bitmap1, 75, 75, false));
             rowView.tv2.setVisibility(1);
             rowView.imgv2.setVisibility(1);
             rowView.tv2.setOnClickListener(op2);
             rowView.imgv2.setOnClickListener(op2);
        }

        opt op = new opt(values.get(position)); 
        rowView.tv1.setOnClickListener(op);
        rowView.imgv1.setOnClickListener(op);
        return convertView;

}

    public class opt implements android.view.View.OnClickListener
    {
        String name;
        public opt(String name)
        {
            this.name = name;
        }
        @Override
        public void onClick(View v) {
            makemenu(name);
        }

    }
}//EndofCustomAdapter1      

1 个答案:

答案 0 :(得分:0)

在getView(),

中更改代码
viewholder rowView;
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
if(convertView == null)
{   
rowView = new viewholder();
convertView = inflater.inflate(R.layout.d_list, parent, false);
rowView.tv1 = (TextView) convertView.findViewById(R.id.textView1); 
rowView.imgv1 = (ImageView) convertView.findViewById(R.id.imageView1);
rowView.tv2 = (TextView) convertView.findViewById(R.id.textView2);
rowView.imgv2 = (ImageView) convertView.findViewById(R.id.imageView2);
rowView.imbg = (ImageView) convertView.findViewById(R.id.imageView3);
convertView.setTag(rowView);            
}
else
{
    rowView = (viewholder) convertView.getTag();
}

希望它会帮助你