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

时间:2018-01-31 08:00:35

标签: java android sqlite android-recyclerview android-glide

我有图像的Sqlite数据库,但它只存储URL,我想滑动处理该URL以显示图像,

案例:我需要使用滑行将图像 url 从sqlite显示到recycleview。

我使用这段代码,因为它让我想到的只是解析数据到字节到图像或其他任何东西。

请帮助,现在我收到错误“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);
    }

错误日志

这就是错误日志。

01-31 14:49:58.795 12577-12577/? E/AndroidRuntime: FATAL EXCEPTION: main
   Process: ptacs.ekatalog.com.e_katalogproduk, PID: 12577
   java.lang.NullPointerException: You cannot start a load on a not yet attached View or a Fragment where getActivity() returns null (which usually occurs when getActivity() is called before the Fragment is attached or after the Fragment is destroyed).
       at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:31)
       at com.bumptech.glide.Glide.getRetriever(Glide.java:665)
       at com.bumptech.glide.Glide.with(Glide.java:697)
       at ptacs.ekatalog.com.e_katalogproduk.adapter.ListAdapter.onBindViewHolder(ListAdapter.java:56)
       at ptacs.ekatalog.com.e_katalogproduk.adapter.ListAdapter.onBindViewHolder(ListAdapter.java:23)
       at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6482)
       at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6515)
       at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5458)
       at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5724)
       at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
       at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
       at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
       at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
       at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
       at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
       at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
       at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410)
       at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3962)
       at android.view.View.layout(View.java:15604)
       at android.view.ViewGroup.layout(ViewGroup.java:4968)
       at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:610)
       at android.view.View.layout(View.java:15604)
       at android.view.ViewGroup.layout(ViewGroup.java:4968)
       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
       at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
       at android.view.View.layout(View.java:15604)
       at android.view.ViewGroup.layout(ViewGroup.java:4968)
       at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:132)
       at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
       at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1361)
       at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:869)
       at android.view.View.layout(View.java:15604)
       at android.view.ViewGroup.layout(ViewGroup.java:4968)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
       at android.view.View.layout(View.java:15604)
       at android.view.ViewGroup.layout(ViewGroup.java:4968)
       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
       at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
       at android.view.View.layout(View.java:15604)
       at android.view.ViewGroup.layout(ViewGroup.java:4968)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
       at android.view.View.layout(View.java:15604)
       at android.view.ViewGroup.layout(ViewGroup.java:4968)
       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
       at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
       at android.view.View.layout(View.java:15604)
    at android.view.View

4 个答案:

答案 0 :(得分:2)

我认为this.mContext = mContext;有问题,可能mContextnull,所以你可以写这个

public ListAdapter(List<Produk> listList,Context mContext){
        this.listList = listList;
        this.mContext = mContext;
    }

确认mContext

答案 1 :(得分:0)

您可以尝试以下代码进行滑行。

//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);
            }
        });
}

答案 2 :(得分:0)

首先在onCreateViewHolder,修复此行:

&#13;
&#13;
ListViewHolder ListViewHolder = new ListViewHolder(view);
return ListViewHolder;
&#13;
&#13;
&#13;

TO:

&#13;
&#13;
return new ListViewHolder(view);
&#13;
&#13;
&#13;

然后在onBindViewHolder中获取如下所示的URI:

&#13;
&#13;
String currentUrl = listList.get(position).getFoto_produk();

Uri uri = Uri.Parse(currentUrl);

Glide.with(mContext).load(uri).into(holder.iv_rFoto);
&#13;
&#13;
&#13;

希望这有助于,同时检查您的getFoto_produk字符串值是否正确。 在将适配器设置为recyclerview

之前,您需要确定此字符串的值

答案 3 :(得分:0)

由于你的mContext是null,你需要将你的上下文传递给Adapter类  通常适配器类是纯java类,没有上下文。

每当您调用Adapter时,您必须从Fragment或Activity传递您的上下文。

首先,在适配器中初始化。

public ListAdapter(List<Produk> listList, Context mContext){
    this.listList = listList;
    this.mContext = mContext;
}

如果你从Fragment调用适配器,那么就这样做

ListAdapter listAdapter = new ListAdapter(listList, getActivity());

如果你从活动中调用适配器,那么就这样做

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

这一切都是好运,只是让我知道。