在Android Studio中使用Glide在适配器类中显示图像时出现问题。

huangapple 未分类评论51阅读模式
英文:

Problem displaying an image in adapter class in android studio using glide

问题

我有两个文本视图和两个图像视图它们需要在特定的帖子中显示文本视图显示正确但图像应该显示的地方出现了空白以下是适配器类

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide; // 导入Glide库
import java.util.List;

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.MyViewHolder> {
    Context mContext;
    List<Advertisement> mData;

    public PostAdapter(Context mContext, List<Advertisement> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View row = LayoutInflater.from(mContext).inflate(R.layout.row_post_item, parent, false);
        return new MyViewHolder(row);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.tvProduct.setText(mData.get(position).getProductName());
        holder.tvDescription.setText(mData.get(position).getProductDescription());
        Glide.with(mContext).load(mData.get(position).getProductImageUrl()).into(holder.imgPost);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvDescription;
        TextView tvProduct;
        ImageView imgPost;
        ImageView imgPostProfile;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvDescription = itemView.findViewById(R.id.row_post_description);
            tvProduct = itemView.findViewById(R.id.row_post_product);
            imgPost = itemView.findViewById(R.id.row_post_img);
            imgPostProfile = itemView.findViewById(R.id.row_post_profile_img);
        }
    }
}

row_post.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="250dp">

    <ImageView
        android:id="@+id/row_post_img"
        android:layout_width="148dp"
        android:layout_height="152dp"
        android:layout_marginTop="50dp"
        android:layout_marginEnd="130dp"
        android:contentDescription="TODO"
        android:scaleType="centerCrop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_launcher_background" />

    <TextView
        android:id="@+id/row_post_description"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="TextView"
        android:textColor="#040404"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/row_post_profile_img"
        app:layout_constraintStart_toStartOf="parent" />

    <ImageView
        android:id="@+id/row_post_profile_img"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        tools:srcCompat="@tools:sample/avatars" />

    <TextView
        android:id="@+id/row_post_product"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:text="TextView"
        android:textColor="#050505"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Advertisement 类:

import androidx.annotation.Keep;

@Keep
public class Advertisement {
    private String productName;
    private String productDescription;
    private String productImageUrl;

    public Advertisement(String productName, String productImageUrl, String productDescription) {
        this.productName = productName;
        this.productImageUrl = productImageUrl;
        this.productDescription = productDescription;
    }

    public Advertisement() {
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getProductDescription() {
        return productDescription;
    }

    public void setProductDescription(String productDescription) {
        this.productDescription = productDescription;
    }

    public String getProductImageUrl() {
        return productImageUrl;
    }

    public void setProductImageUrl(String productImageUrl) {
        this.productImageUrl = productImageUrl;
    }
}

请注意,我在适配器类中添加了 Glide 图像加载库,以加载图像到 ImageView 中。这应该有助于解决您在图像显示方面遇到的问题。

英文:

I have two text views and two image views that has to show up in a particular post. The text View shows up correctly, but I see a blank space where the image should be.
Here is the adapter class :

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class PostAdapter extends RecyclerView.Adapter&lt;PostAdapter.MyViewHolder&gt; {
    Context mContext;
    List&lt;Advertisement&gt; mData ;
    Uri imguri;
    String url;
    public PostAdapter(Context mContext, List&lt;Advertisement&gt; mData) {
        this.mContext = mContext;
        this.mData = mData;
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View row = LayoutInflater.from(mContext).inflate(R.layout.row_post_item,parent,false);
        return new MyViewHolder(row);
    }
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.tvProduct.setText(mData.get(position).getProductName());
        holder.tvDescription.setText(mData.get(position).getProductDescription());
        /*url=mData.get(position).getProductImageUrl();
        imguri = Uri.parse(url);
        holder.imgPost.setImageURI(imguri);*/
        Glide.with(mContext).load(mData.get(position).getProductImageUrl()).into(holder.imgPost);
    }
    @Override
    public int getItemCount() {
        return mData.size();
    }
    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvDescription;
        TextView tvProduct;
        ImageView imgPost;
        ImageView imgPostProfile;
        public MyViewHolder(View itemView) {
            super(itemView);
            tvDescription = itemView.findViewById(R.id.row_post_description);
            tvProduct = itemView.findViewById(R.id.row_post_product);
            imgPost = itemView.findViewById(R.id.row_post_img);
            imgPostProfile = itemView.findViewById(R.id.row_post_profile_img);
           
        }
    }
}

And this is the row_post.xml file :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;250dp&quot;&gt;


    &lt;ImageView
        android:id=&quot;@+id/row_post_img&quot;
        android:layout_width=&quot;148dp&quot;
        android:layout_height=&quot;152dp&quot;
        android:layout_marginTop=&quot;50dp&quot;
        android:layout_marginEnd=&quot;130dp&quot;
        android:contentDescription=&quot;TODO&quot;
        android:scaleType=&quot;centerCrop&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        app:srcCompat=&quot;@drawable/ic_launcher_background&quot; /&gt;


    &lt;TextView
        android:id=&quot;@+id/row_post_description&quot;
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;16dp&quot;
        android:layout_marginEnd=&quot;8dp&quot;
        android:layout_marginBottom=&quot;8dp&quot;
        android:text=&quot;TextView&quot;
        android:textColor=&quot;#040404&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toStartOf=&quot;@+id/row_post_profile_img&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;

    &lt;ImageView
        android:id=&quot;@+id/row_post_profile_img&quot;
        android:layout_width=&quot;48dp&quot;
        android:layout_height=&quot;48dp&quot;
        android:layout_marginEnd=&quot;16dp&quot;
        android:layout_marginBottom=&quot;8dp&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        tools:srcCompat=&quot;@tools:sample/avatars&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/row_post_product&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;16dp&quot;
        android:layout_marginTop=&quot;16dp&quot;
        android:text=&quot;TextView&quot;
        android:textColor=&quot;#050505&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

Note : I checked that I do get a url through the method getProductImageUrl() .
Please help me on how to rectify this problem. Been stuck on it for a while now

This is the Advertisement class

import androidx.annotation.Keep;

@Keep
public class Advertisement {
    private String productName;
    private String productDescription;
    private String productImageUrl;
    public Advertisement(String productName,String productImageUrl,String productDescription)
    {
        this.productName=productName;
        this.productImageUrl=productImageUrl;
        this.productDescription=productDescription;
    }
    public Advertisement()
    {
    }


    @Override
    public String toString() {
        return &quot;Advertisement{&quot; +
                &quot;product_Name=&#39;&quot; + productName + &#39;\&#39;&#39; +
                &quot;, product_Image_URL=&#39;&quot; + productImageUrl + &#39;\&#39;&#39; +
                &quot;, product_Description=&#39;&quot; + productDescription +
                &#39;}&#39;;
    }
    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getProductDescription() {
        return productDescription;
    }

    public void setProductDescription(String productDescription) {
        this.productDescription = productDescription;
    }

    public String getProductImageUrl() {
        return productImageUrl;
    }

    public void setProductImageUrl(String productImageUrl) {
        this.productImageUrl = productImageUrl;
    }
}

Here is a screenshot of the database
在Android Studio中使用Glide在适配器类中显示图像时出现问题。

huangapple
  • 本文由 发表于 2020年7月25日 00:10:28
  • 转载请务必保留本文链接:https://java.coder-hub.com/63077412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定