2013-04-28 35 views
1

我正在处理电子商务应用程序,我需要从数据库中显示产品列表-a产品=一个ImageView和一些textViews-,但是当我提取来自数据库的数据,除了imageView以外,它们可以正常工作,它显示了与源布局相同的图像。无法更改我的列表视图中的图像源

这是我的适配器中的getView()方法。

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

    ViewHolder holder; 
    if (convertView==null) 
    { 
     holder=new ViewHolder(); 
     convertView = inflater.inflate(R.layout.lesproduits, null); 
     holder.nomduProduit = (TextView)convertView.findViewById(R.id.nomProduit); 
     holder.prixDuProduit = (TextView)convertView.findViewById(R.id.prixProduit); 
     holder.imageDuProduit = (ImageView)convertView.findViewById(R.id.imageProduit); 
     convertView.setTag(holder); 
    } 

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

    Bitmap bitmapImage = BitmapFactory.decodeFile(path+File.separator+lesProduits.get(position).getImage()); 
    Drawable drawableImage = new BitmapDrawable(bitmapImage); 
    System.out.println(path+File.separator+lesProduits.get(position).getImage()); 

    holder.imageDuProduit.setBackgroundDrawable(drawableImage); 
    holder.nomduProduit.setText(lesProduits.get(position).getNomDuProduit()); 
    holder.prixDuProduit.setText(lesProduits.get(position).getPrixDuProduit()); 
    return convertView; 
} 

以下是源布局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ImageView 
    android:id="@+id/imageProduit" 
    android:layout_width="100dp" 
    android:layout_height="50dp" 
    android:layout_marginLeft="5dp" 
    android:layout_marginRight="30dp" 
    android:layout_marginTop="5dp" 
    android:src="@drawable/ic_launcher" /> 

<TextView 
    android:id="@+id/nomProduit" 
    android:layout_marginTop="5dp" 
    android:layout_width="170dp" 
    android:layout_height="30dp" 
    android:layout_toRightOf="@+id/imageProduit" 
    android:text="Smart phone" 
    android:textSize="25sp" /> 

<TextView 
    android:id="@+id/prixProduit" 
    android:layout_width="100dp" 
    android:layout_height="30dp" 
    android:layout_alignLeft="@+id/nomProduit" 
    android:layout_below="@+id/nomProduit" 
    android:layout_marginTop="5dp" 
    android:text="Large Text" 
    android:textSize="15sp" /> 

</RelativeLayout> 

回答

0

你正在改变的背景下,没有前景。 ImageView具有前后图像。

,而不是这个

holder.imageDuProduit.setBackgroundDrawable(drawableImage); 

使用本

holder.imageDuProduit.setImageDrawable(drawableImage); 
+0

是的,就是这样,现在一切工作正常谢谢。 – 2013-04-28 14:14:43