timusus / RecyclerView-FastScroll

A simple FastScroller for Android's RecyclerView
Other
1.39k stars 183 forks source link

How do I modify each Item's Images (similar to readme screenshot)? #106

Closed chirpphixel closed 4 years ago

chirpphixel commented 4 years ago

Currently the sample project only shows duplicating the same jigsaw puzzle icon. How do I change the Images for each item dynamically?

I tried adding a ImageView variable under Adapter like this

static class ViewHolder extends RecyclerView.ViewHolder { public TextView text; public ImageView imgview = (ImageView) itemView.findViewById(R.id.collectionImage);

    ViewHolder(View itemView) {
        super(itemView);
        text = itemView.findViewById(R.id.text);
        imgview = itemView.findViewById(R.id.collectionImage);
    }
}

and then set ImageDrawable

@SuppressLint("DefaultLocale") @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.text.setText(getNameForItem(position)); holder.imgview.setImageDrawable(getImageForItem(position)); }

I ran into this error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference

timusus commented 4 years ago

This library is for adding a FastScroller to the side of the RecyclerView. What you're asking about is how to use a RecyclerView in general. You'd probably be better off asking this over at StackOverflow.

It looks like you're calling setImageResource somewhere, on a null ImageView.