【问题标题】:Can't change Image source in my listview无法在我的列表视图中更改图像源
【发布时间】:2023-03-08 09:26:01
【问题描述】:

我正在开发一个电子商务应用程序,我需要从数据库中显示一个产品列表 - 一个产品 = 一个 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>

【问题讨论】:

    标签: android sqlite android-listview


    【解决方案1】:

    您正在更改背景,而不是前景。 ImageView 有前景和背景图像。

    而不是这个

      holder.imageDuProduit.setBackgroundDrawable(drawableImage);
    

    使用这个

     holder.imageDuProduit.setImageDrawable(drawableImage);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 1970-01-01
      • 2011-09-25
      • 2020-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多