我有非常正常的recyclerview适配器。我将数组列表传递给适配器,并根据需要尝试使用Glide设置图像。
问题是,当我尝试将图像链接到loggin时,它们似乎正常,但是当我将数据列表传递给适配器时,它们无法正常工作。并给予内存对象而不是String。
我应该得到这个: http://somelin.com/image.jpg
相反,我得到了这个: package.name.models.responses.chain.Logo_@c170923
我该怎么办?我的适配器正常工作,因为我在其他地方以相同的方式使用此适配器并且它可以工作。
我的适配器
public class ChainAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private List<Restaurant> modelList;
private OnItemClickListener mItemClickListener;
public ChainAdapter(Context context, ArrayList<Restaurant> modelList) {
this.mContext = context;
this.modelList = modelList;
}
public void updateList(ArrayList<Restaurant> modelList) {
this.modelList = modelList;
notifyDataSetChanged();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_chain_list, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
//Here you can fill your row view
if (holder instanceof ViewHolder) {
final Restaurant model = getItem(position);
ViewHolder genericViewHolder = (ViewHolder) holder;
String link = "http://cdn.link.in/unsafe/" + model.getLogo();
System.out.println("chainAdapter " + link);
Glide.with(mContext).load(link).into(genericViewHolder.rest_logo);
CenteredImageSpan imagespan = new CenteredImageSpan(mContext, R.drawable.verified_account);
Spannable text = new SpannableString(model.getName() + " ");
text.setSpan(imagespan, model.getName().length(), model.getName().length() + 1, 0); //text is an object of TextView
if (model.getVerified()) {
genericViewHolder.rest_name.setText(text);
} else {
genericViewHolder.rest_name.setText(model.getName());
}
Calendar today = Calendar.getInstance();
List<Today> hours = model.getToday();
for (Today hour : hours) {
if (hour.getWeekday() == today.get(Calendar.DAY_OF_WEEK) - 1) {
genericViewHolder.restCloseNowIcon.setImageResource(R.drawable.ic_open_now);
genericViewHolder.restCloseNowText.setText(mContext.getString(R.string.open_now));
genericViewHolder.restCloseNowText.setTextColor(mContext.getResources().getColor(R.color.open_now));
} else {
genericViewHolder.restCloseNowIcon.setImageResource(R.drawable.ic_close_now);
genericViewHolder.restCloseNowText.setText(mContext.getString(R.string.close_now));
genericViewHolder.restCloseNowText.setTextColor(mContext.getResources().getColor(R.color.close_now));
}
}
int totalRatings = model.getTotalRatings();
if (totalRatings != 0)
genericViewHolder.restStars.setText((int) totalRatings + "");
}
}
@Override
public int getItemCount() {
return modelList.size();
}
public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
private Restaurant getItem(int position) {
return modelList.get(position);
}
public interface OnItemClickListener {
void onItemClick(View view, int position, Restaurant model);
}
public class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.imageView8)
ImageView rest_logo;
@BindView(R.id.textView3)
TextView rest_name;
@BindView(R.id.isVerified)
ImageView isVerified;
@BindView(R.id.rest_close_now_icon)
ImageView restCloseNowIcon;
@BindView(R.id.rest_close_now_text)
TextView restCloseNowText;
@BindView(R.id.rest_stars)
TextView restStars;
public ViewHolder(final View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
itemView.setOnClickListener(view -> mItemClickListener.onItemClick(itemView, getAdapterPosition(), modelList.get(getAdapterPosition())));
}
}
}
模型
public class Restaurant implements Parcelable
{
@SerializedName("username")
@Expose
private String username;
@SerializedName("total_ratings")
@Expose
private Integer totalRatings;
@SerializedName("verified")
@Expose
private Boolean verified;
@SerializedName("name")
@Expose
private String name;
@SerializedName("logo")
@Expose
private Logo_ logo;
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("today")
@Expose
private List<Today> today = null;
@SerializedName("thumbnail")
@Expose
private Thumbnail thumbnail;
public final static Creator<Restaurant> CREATOR = new Creator<Restaurant>() {
@SuppressWarnings({
"unchecked"
})
public Restaurant createFromParcel(Parcel in) {
return new Restaurant(in);
}
public Restaurant[] newArray(int size) {
return (new Restaurant[size]);
}
}
;
protected Restaurant(Parcel in) {
this.username = ((String) in.readValue((String.class.getClassLoader())));
this.totalRatings = ((Integer) in.readValue((Integer.class.getClassLoader())));
this.verified = ((Boolean) in.readValue((Boolean.class.getClassLoader())));
this.name = ((String) in.readValue((String.class.getClassLoader())));
this.logo = ((Logo_) in.readValue((Logo_.class.getClassLoader())));
this.id = ((Integer) in.readValue((Integer.class.getClassLoader())));
in.readList(this.today, (Today.class.getClassLoader()));
this.thumbnail = ((Thumbnail) in.readValue((Thumbnail.class.getClassLoader())));
}
/**
* No args constructor for use in serialization
*
*/
public Restaurant() {
}
/**
*
* @param id
* @param logo
* @param username
* @param thumbnail
* @param verified
* @param name
* @param today
* @param totalRatings
*/
public Restaurant(String username, Integer totalRatings, Boolean verified, String name, Logo_ logo, Integer id, List<Today> today, Thumbnail thumbnail) {
super();
this.username = username;
this.totalRatings = totalRatings;
this.verified = verified;
this.name = name;
this.logo = logo;
this.id = id;
this.today = today;
this.thumbnail = thumbnail;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getTotalRatings() {
return totalRatings;
}
public void setTotalRatings(Integer totalRatings) {
this.totalRatings = totalRatings;
}
public Boolean getVerified() {
return verified;
}
public void setVerified(Boolean verified) {
this.verified = verified;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Logo_ getLogo() {
return logo;
}
public void setLogo(Logo_ logo) {
this.logo = logo;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<Today> getToday() {
return today;
}
public void setToday(List<Today> today) {
this.today = today;
}
public Thumbnail getThumbnail() {
return thumbnail;
}
public void setThumbnail(Thumbnail thumbnail) {
this.thumbnail = thumbnail;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(username);
dest.writeValue(totalRatings);
dest.writeValue(verified);
dest.writeValue(name);
dest.writeValue(logo);
dest.writeValue(id);
dest.writeList(today);
dest.writeValue(thumbnail);
}
public int describeContents() {
return 0;
}
}
模型标志_
public class Logo_ implements Parcelable {
@SerializedName("path")
@Expose
private String path;
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("caption")
@Expose
private String caption;
public final static Creator<Logo_> CREATOR = new Creator<Logo_>() {
@SuppressWarnings({
"unchecked"
})
public Logo_ createFromParcel(Parcel in) {
return new Logo_(in);
}
public Logo_[] newArray(int size) {
return (new Logo_[size]);
}
}
;
protected Logo_(Parcel in) {
this.path = ((String) in.readValue((String.class.getClassLoader())));
this.id = ((Integer) in.readValue((Integer.class.getClassLoader())));
this.caption = ((String) in.readValue((String.class.getClassLoader())));
}
/**
* No args constructor for use in serialization
*
*/
public Logo_() {
}
/**
*
* @param id
* @param path
* @param caption
*/
public Logo_(String path, Integer id, String caption) {
super();
this.path = path;
this.id = id;
this.caption = caption;
}
public String getPath() {
return this.path;
}
public void setPath(String path) {
this.path = path;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(path);
dest.writeValue(id);
dest.writeValue(caption);
}
public int describeContents() {
return 0;
}
}
答案 0 :(得分:1)
正如您所发现的那样,问题出现在这一行:
String link = "YOUR_URL" + model.getLogo();
您的方法getLogo()不返回要在此处附加的字符串,而是返回类型为Logo_
的对象。您有3个选择:
更改模型中的方法getLogo以返回String(可能是徽标文件名)。
在Logo对象中,创建另一个方法以返回名称并按以下方式调用它:
String link = "YOUR_URL" + model.getLogo().getFilename();
3.在Logo对象中更改toString方法以返回文件名。
@Override
public String toString(){
return this.filename;
}
编辑:您可以在徽标POJO中使用getPath():
String link = "YOUR_URL" + model.getLogo().getPath();