将第一行和最后一行设置为自定义drawable

时间:2012-12-20 03:30:07

标签: android listview custom-adapter

我试图让它成为我的listview的最后一行和第一行将有圆角。现在我只想让第一行有圆角。我能够完成这个但是一些随机的行通常是5-7和15或16行也将被设置为圆形边缘背景,我无法弄清楚原因。

以下是我adapter code的一部分,我尝试将圆边设置为drawable:

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

    if (convertView == null) {
        vi = inflater.inflate(R.layout.row, null);
        holder = new ViewHolder();
        holder.title = (TextView) vi.findViewById(R.id.title);
        holder.desc = (TextView) vi.findViewById(R.id.details);
        holder.date = (TextView) vi.findViewById(R.id.date);
        holder.price = (TextView) vi.findViewById(R.id.price);
        holder.location = (TextView) vi.findViewById(R.id.location);
        holder.newImage = (ImageView) vi.findViewById(R.id.savedNewItemsRibbon);
        holder.rellay = (RelativeLayout) vi.findViewById(R.id.widget30);

        //holder.image = (ImageView) vi.findViewById(R.id.thumb);
        vi.setTag(holder);
    }else{
        holder = (ViewHolder) vi.getTag();
    }
    if(position == 0 && firstRow){
        holder.rellay.setBackgroundResource(R.drawable.roundededges);
        firstRow = false;
    }else{
        //vi.setBackgroundColor(Color.WHITE);
    }


    Log.d("MySQL", "COunt:"+position);
    holder.price.setText(data.get(position).getPrice());
    holder.location.setText(data.get(position).getUrl());

    datasource = new PostDataSource(activity);
    datasource.open();

    if(datasource.checkIfNew(data.get(position).getTitle())){
        holder.newImage.setVisibility(View.VISIBLE);
        //vi.setBackgroundColor(Color.YELLOW);

    }else{
        holder.title.setTextColor(Color.BLACK);
    }
    holder.title.setText(data.get(position).getTitle());

    holder.desc.setText(data.get(position).getDescription());
    holder.date.setText(data.get(position).getPubDate());
    holder.location.setText(data.get(position).getCity());


    //holder.title.setText(data.get(0).getTitle());
    //imageLoader.DisplayImage((data.get(position).getThumbnail()), activity,holder.image, 72, 72);


    URL url = null;
    try {
        url = new URL((data.get(position).getThumbnail()));
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    InputStream content = null;


    /*
    try {
        //content = (InputStream)url.getContent();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    Drawable d = Drawable.createFromStream(content , "src"); 
    Bitmap mIcon1 = null;
     try {
         mIcon1 =
                BitmapFactory.decodeStream(url.openConnection().getInputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }
    holder.image.setImageBitmap(Bitmap.createScaledBitmap(mIcon1, 72, 72, false));
     */
    positionCount++;
    return vi;

}

我尝试过使用position == 0将其设置为无效。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

这很可能是因为框架正在将非空convertView传递给您的getView方法,该方法恰好来自您的第一行。

最简单的解决方法是取消设置除第一行之外的每一行的后台资源:

   if (position == 0){
       holder.rellay.setBackgroundResource(R.drawable.roundededges);
   } else {
       holder.rellay.setBackgroundResource(0);
   }