setImage ImageAdapter Android

时间:2016-10-24 13:51:59

标签: java android json android-adapter android-viewholder

我下载了JSON数据并成功转换为POJO对象。现在,我有一个问题,"如何将图像从URL设置为视图持有者的imagivew属性?"

ImagesAdapter.java:

package gc.x;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import gc.ovidos_challenge.R;
import gc.ovidos_challenge.models.Image;

import java.net.URL;
import java.util.List;


public class ImagesAdapter extends RecyclerView.Adapter<ImagesAdapter.MyViewHolder> {

    private List<Image> imagesList;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView title;
        public ImageView imageview;

        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.textview);
            imageview = (ImageView) view.findViewById(R.id.imageurl);

        }
    }


    public ImagesAdapter(List<Image> imagesList) {
        this.imagesList = imagesList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_image, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Image image = imagesList.get(position);
        holder.title.setText(image.title);

      //  holder.imageview.setImage(image.url); //Problem is here, How can I implement?


    }

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


}

我无法想象如何实现 setImage 方法。

图片类:

public class Image {

    public String albumId;
    public String id;
    public String title;
    public String url;
    public String thumbnailUrl;

}

2 个答案:

答案 0 :(得分:0)

wait.Until(ExpectedConditions.ElementExists(By.XPath("//*[@id='layerContainer']/div/section/div/div[2]/form/article/footer/div/button")));
IWebElement submit2 = this.driver.FindElement(By.XPath("//*[@id='layerContainer']/div/section/div/div[2]/form/article/footer/div/button"));
submit2.Click();

并编译 Picasso.with(context).load(image.url).into(//your_image_view );

答案 1 :(得分:0)

不使用外部资源库

new DownloadImageTask(imageview).execute(url);

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> 
{
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

使用Picasso

Picasso.with(context).load(image.url).into(//your_image_view );

并编译compile 'com.squareup.picasso:picasso:2.5.2'

Universal Image Loader Lib。轻量级。快速且易于使用。 See this Link