英文:
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<PostAdapter.MyViewHolder> {
Context mContext;
List<Advertisement> mData ;
Uri imguri;
String url;
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());
/*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 :
<?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>
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 "Advertisement{" +
"product_Name='" + productName + '\'' +
", product_Image_URL='" + productImageUrl + '\'' +
", product_Description='" + productDescription +
'}';
}
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;
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论