Java 如何使用Glide向RecycleView显示图像?

Java 如何使用Glide向RecycleView显示图像?,java,android,sqlite,android-recyclerview,android-glide,Java,Android,Sqlite,Android Recyclerview,Android Glide,我有用于图像的Sqlite数据库,但它只存储URL,我希望glide处理该URL以显示图像 案例:我需要使用glide显示从sqlite到recycleview的图像url 我之所以使用这段代码,是因为它比将数据从一个字节解析到另一个图像或者其他任何东西都要简单 请帮助,现在我得到了错误Java.lang.nullPointerException public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListVi

我有用于图像的Sqlite数据库,但它只存储URL,我希望glide处理该URL以显示图像

案例:我需要使用glide显示从sqlite到recycleview的图像url

我之所以使用这段代码,是因为它比将数据从一个字节解析到另一个图像或者其他任何东西都要简单

请帮助,现在我得到了错误Java.lang.nullPointerException

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder> {

    private Context mContext;

    private List<Produk> listList = new ArrayList<>();

    public ListAdapter(List<Produk> listList){

        this.listList = listList;
        this.mContext = mContext;
    }

    @Override
    public ListAdapter.ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_list ,parent, false);

        ListViewHolder ListViewHolder = new ListViewHolder(view);
        return ListViewHolder;
    }

    @Override
    public void onBindViewHolder(ListAdapter.ListViewHolder holder, int position) {

        holder.txt_rKode.setText((listList.get(position).getKd_produk()));
        holder.txt_rNama.setText(listList.get(position).getNama_produk());
        holder.txt_rMerk.setText(listList.get(position).getMerk_produk());
        holder.txt_rJenis.setText(listList.get(position).getJenis_produk());
        holder.txt_rVariasi.setText(listList.get(position).getVariasi_produk());
        holder.txt_rFoto2.setText(listList.get(position).getFoto_produk());

        String currentUrl = listList.get(position).getFoto_produk();

        Glide.with(mContext)
                .load(currentUrl)
                .into(holder.iv_rFoto);




    }

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

    public Produk getItem(int Position) {
        return listList.get(Position);
    }

你可以试试下面的glide代码

//Glide to load images from url 
if(currentUrl!=null) { 

    //Check whether the url is null   
    Glide.with(mContext)
        .load(currentUrl)//Pass url to be appended
        .asBitmap()
        .fitCenter()
        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
        .placeholder(ContextCompat.getDrawable(mContext, R.drawable.ic_dummy_user))
        .error(ContextCompat.getDrawable(mContext, R.drawable.ic_dummy_user))
        .into(new SimpleTarget<Bitmap>() {

            @Override
            public void onResourceReady(Bitmap bitmapp, GlideAnimation<? super Bitmap> glideAnimation) {
                mMarkerImageView.setImageBitmap(bitmap);
            }
        });
}
我认为这个.mContext=mContext;是有问题的,也许mContext是空的,所以你可以写这个

public ListAdapter(List<Produk> listList,Context mContext){
        this.listList = listList;
        this.mContext = mContext;
    }
要首先在onCreateViewHolder中确认mContext,请修复此行:

ListViewHolder ListViewHolder=新建ListViewHolderview;
返回ListViewHolder 由于您的mContext为空,您需要将您的上下文传递给适配器类 通常适配器类是纯java类,没有上下文

无论何时调用适配器,都必须从片段或活动传递您的上下文

首先,在适配器中进行如下初始化

public ListAdapter(List<Produk> listList, Context mContext){
    this.listList = listList;
    this.mContext = mContext;
}
如果您从活动中调用适配器,请执行以下操作

ListAdapter listAdapter = new ListAdapter(listList, getActivity());
ListAdapter listAdapter = new ListAdapter(listList, YourActivity.this);

祝你好运,请告诉我。

请同时发布错误日志。请发布堆栈跟踪。你能解释错误吗。你的上下文为空。您不向适配器传递上下文,请在此处添加图像url和活动或片段。复制粘贴其他人的答案时,请尝试编辑注释。在activity_list.xml处,会出现新错误错误错误:类ListAdapter中的构造函数ListAdapter无法应用于给定类型;必需:List,Context found:List原因:实际参数列表和形式参数列表的长度@MostoDani不同,这是因为您必须将Context作为第二个参数传递,正如在方法中声明的那样。您需要从外部传递上下文,就像此新ListAdapterlist、此或新ListAdapterlist一样,getActivity决定您是在活动中还是在片段中。你可以试试。希望能帮你。是的,伙计,这是错误的:ListViewHolder ListViewHolder=new ListViewHolderview;它没有做任何改变,同样的错误。我已经用Txt\U rFoto检查了字符串输出,它显示了url并禁用了Imageview。在初始化Imageview应用程序崩溃并返回到父活动OK后,问题出在您的片段中。检查您是否在片段中的视图之前使用了getActivity。您知道什么,也上传您的片段。如果你找不到问题,我没有使用碎片。不是崩溃,而是新问题:D。加载www.ellenmacarthurfoundation.org/assets/images/ce100/Phillips-logo.jpg失败,大小为[250x250]class java.io.FileNotFoundException/www.ellenmacarthurfoundation.org/assets/images/ce100/Phillips-logo.jpg:open failed:enoon没有这样的文件或目录我没有收到您的详细信息,请发布,附加错误日志。我认为上下文问题解决了您现在面临的另一个问题,然后正确地发布您的问题。请先使用测试图像url进行测试,然后使用此url[
ListAdapter listAdapter = new ListAdapter(listList, YourActivity.this);