我有一个recyclerview.I我试图水平加载图像,但我不断得到foll错误
You must provide a Model of a type for which there is a registered ModelLoader, if you are using a custom model, you must first call Glide#register with a ModelLoaderFactory for your custom model class
我尝试了很多但是无法得到一个对我有帮助的答案。以下是我的代码
public class Images {
public String[] imageUrl;
public String[] getImageUrl() {
return imageUrl;
}
public void setImageUrl(String[] imageUrl) {
this.imageUrl = imageUrl;
}
public Images(String[] imageUrl) {
this.imageUrl = imageUrl;
}
}
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private Context context;
private List<Images> imagesList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView recyclerimageView;
public MyViewHolder(View view) {
super(view);
recyclerimageView=(ImageView)view.findViewById(R.id.recyclerlistitemimageview);
}
}
public RecyclerAdapter(Context context,List<Images> imagesList){
this.context=context;
this.imagesList=imagesList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerlistitem, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
// Glide.with(context).load(myarr).into(holder.recyclerimageView);
Images images=imagesList.get(position);
Glide.with(context).load(images.getImageUrl()).into(holder.recyclerimageView);
}
@Override
public int getItemCount() {
return imagesList.size();
// return myarr.length;
}
}
public class Arrayclass {
public static String activa3gimage3[]={"https://firebasestorage.googleapis.com/v0/b/venetian-honda-179411.appspot.....",
"https://firebasestorage.googleapis.......",
"https://firebasestorage.googleapis....",
"https://firebasestorage.googleapis......"};
}
我已将我的图像上传到服务器并在activa3gimage3数组中获取图像
public class ModelLineUpInnerActivity extends AppCompatActivity {
private List<Images> imagesList = new ArrayList<>();
private RecyclerView recyclerView;
private RecyclerAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_model_line_up_inner);
recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
Images images=new Images(Arrayclass.activa3gimage3);
imagesList.add(images);
mAdapter = new RecyclerAdapter(ModelLineUpInnerActivity.this,imagesList);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
}
}
我知道错误是因为Glide,我正在尝试将图像加载到其中,但我不知道如何解决它。任何帮助都将受到赞赏。
答案 0 :(得分:2)
您需要使用单个网址下载并使用带索引的数组将图片加载到单个图像中,因此请使用
Glide.with(context).load(images.getImageUrl()[position]).into(holder.recyclerimageView);
// ^^^^^^^^^^
更新:
您的列表只包含单个项目,因此您需要修改包含其中所有项目的列表
Images images=new Images(Arrayclass.activa3gimage3);
imagesList.add(images); // single item so single view
使用List<String>
并消除Images
的使用,因为您的适配器只需要字符串url
来加载图片。
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private Context context;
private List<String> imagesList;
// use String list
public RecyclerAdapter(Context context,List<String> imagesList){
this.context=context;
this.imagesList=imagesList;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Glide.with(context).load(imagesList.get(position)).into(holder.recyclerimageView);
}
@Override
public int getItemCount() {
return imagesList.size();
}
// ... code
和设置适配器一样
private List<String> imagesList = new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_model_line_up_inner);
recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
imagesList.addAll(new ArrayList<>(Arrays.asList(Arrayclass.activa3gimage3)));
mAdapter = new RecyclerAdapter(ModelLineUpInnerActivity.this,imagesList);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
}
答案 1 :(得分:1)
images.getImageUrl()
是数组,您需要String
包含图片的网址
使用List<String>
代替List<Images>
(也在RecyclerAdapter
&amp; ModelLineUpInnerActivity
更新
private List<String> imagesList = new ArrayList<>();
在此列表中添加您的网址,如下所示:
imagesList.addAll(Arrays.asList(Arrayclass.activa3gimage3));
之后您可以加载图片:
String image = imagesList.get(position);
Glide.with(context).load(image).into(holder.recyclerimageView);