Textview背景在列表视图中重复

时间:2013-06-08 16:36:44

标签: android listview background textview

所以我面临的问题是,当我尝试动态设置属于listView的textView的背景时,我设置的背景也会为其他textViews设置..它会重复...我看到listView项目的这些问题很常见,但找不到我的解决方案

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

         ViewHolder holder;

         if (convertView == null) {
             convertView = mInflater.inflate(R.layout.product_item, null);

             holder = new ViewHolder();

             holder.increment = (Button) convertView.findViewById(R.id.increment);
             holder.decrement = (Button) convertView.findViewById(R.id.decrement);
             holder.pic_view_quantity = (TextView) convertView.findViewById(R.id.pic_view_quantity);

             convertView.setTag(holder);

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



         holder.pic_view_quantity.setText(products.get(position).getQuantity());
         if (products.get(position).getQuantity().matches("[0-9]+")){
             holder.pic_view_quantity.setBackgroundResource(R.drawable.transparent_rectangle); //this is where i set the background, to items that have a quantity
         }

         return convertView;
     }

我的布局:

 <RelativeLayout 
     android:id="@+id/relativelayout"
     android:layout_width="70dp"
     android:layout_height="70dp" >

    <ImageView android:id="@+id/pic_view"  
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_marginTop="5sp"
        android:contentDescription="@string/product_picture"  >

    </ImageView>

    <TextView
       android:id="@+id/pic_view_quantity"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignLeft="@+id/pic_view"
       android:layout_alignTop="@+id/pic_view"
       android:layout_alignRight="@+id/pic_view"
       android:layout_alignBottom="@+id/pic_view"
       android:layout_margin="1dp"
       android:textSize="36sp"
       android:gravity="center"
       android:textColor="#FF0000" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

else添加到以下语句

if (products.get(position).getQuantity().matches("[0-9]+")){
    holder.pic_view_quantity.setBackgroundResource(R.drawable.transparent_rectangle); //this is where i set the background, to items that have a quantity
}
else {
    holder.pic_view_quantity.setBackgroundResource(0); 
}