如何在android中的json响应中在列表视图中显示图像 [英] How to display image in a listview from json response in android

查看:129
本文介绍了如何在android中的json响应中在列表视图中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我正在使用改型库显示json响应中的数据. 使用recylerview会显示标题,id,相册以及image.all都在显示,除了image.

Hi in the below code am displaying data from json response using retrofit library. using recylerview am displaying title,id,albumid as well as image.all are displaying except image .

但是如何显示来自url的图像.谁能帮帮我吗 这是我的适配器类.

But how to display image from url. can any one please help me this is my adapter class.

.java

public class RecyclerViewAdapterForAlbum extends RecyclerView.Adapter<RecyclerViewAdapterForAlbum.ViewHolder> {

    private List<UserAlbum> item;
    Context context ;

    public RecyclerViewAdapterForAlbum(Context context, List<UserAlbum> item ) {
        Log.d("123", "RecyclerViewAdapter");
        this.item = item;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d("123", "onCreateViewHolder");
        View view = LayoutInflater.from(context).inflate(R.layout.activity_second, null);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Log.d("123", "onBindViewHolder");
        holder.user_id.setText(String.valueOf(item.get(position).getId()));
        holder.album_id.setText(String.valueOf(item.get(position).getAlbumId()));
        holder.title.setText(item.get(position).getTitle());
        //Bitmap getBitMapFromUrl=null;
       holder.imageForText.setImageResource(item.get(position).getThumbnailUrl());
    }

    @Override
    public int getItemCount() {
        Log.d("123", "getItemCount");
        return item.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView album_id, user_id,title;
        public ImageView imageForText;

        public ViewHolder(View itemView) {
            super(itemView);
            Log.d("123", "ViewHolder");


            user_id = (TextView) itemView.findViewById(R.id.user_id);
            album_id=(TextView)itemView.findViewById(R.id.album_id);
            title=(TextView)itemView.findViewById(R.id.title);
            imageForText=(ImageView)itemView.findViewById(R.id.img_id);


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    int pos = getAdapterPosition();
//                    int userID=item.get(pos).getId();
//                    Intent myIntent = new Intent(v.getContext(), SecondActivity.class);
//                    myIntent.putExtra("userId",userID);
//                    context.startActivity(myIntent);

                }
            });
        }


    }
}

推荐答案

使用毕加索:

将此行添加到您的应用级build.gradle文件中.

Add this line to your app-level build.gradle file.

implementation 'com.squareup.picasso:picasso:2.71828'

您可以通过以下方式将图像设置为ImageView:

And you can simply set image to ImageView by:

Picasso.get().load(item.get(position).getThumbnailUrl()).into(holder.imageForText);

使用Glide:

将这些行添加到您的应用级build.gradle文件中.

Add these lines to your app-level build.gradle file.

implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'

您可以通过以下方式将图像设置为ImageView:

And you can simply set image to ImageView by:

GlideApp.with(this).load(item.get(position).getThumbnailUrl()).into(holder.imageForText);

这篇关于如何在android中的json响应中在列表视图中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆