0

所以我面临的问题是,当我尝试动态设置属于 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>
4

1 回答 1

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); 
}
于 2013-06-08T16:41:36.500 回答