0

I want to show images from array in recyclerview so I pass array to adapter but recyclerview does not bind to adapter because I used "Log.d" in "onBindViewHolder" but Log doesn't show message.Whats wrong with my code?Is there other solution?

in fragment:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_plan, container, false);

    paths[0] = "/storage/emulated/0/demonuts_upload_gallery/1543839716755.jpg";
    paths[1] = "/storage/emulated/0/demonuts_upload_gallery/1543839716755.jpg";
    paths[2] = "/storage/emulated/0/demonuts_upload_gallery/1543839716755.jpg";
    paths[3] = "/storage/emulated/0/demonuts_upload_gallery/1543839716755.jpg";
    paths[4] = "/storage/emulated/0/demonuts_upload_gallery/1543839716755.jpg";

    adapter = new ImagesBodyAdapter(view.getContext());
    LinearLayoutManager layoutManager = new LinearLayoutManager(view.getContext(),LinearLayoutManager.HORIZONTAL, false);
    recyclerView = view.findViewById(R.id.recycler);
    recyclerView.setLayoutManager(layoutManager);
    adapter.addItems(paths);
    adapter.notifyDataSetChanged();
    recyclerView.setAdapter(adapter);

Adapter class is:

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

private String[] PATHS = new String[5];

private Context context;

public ImagesBodyAdapter(Context context){
    this.context = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.image_body_item,viewGroup,false);
    return new ViewHolderItemDetail(view);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
    //ViewHolderItemDetail holder = (ViewHolderItemDetail) viewHolder;

    Log.d("adapter","recycler binded!");

    for (int counter=0; counter<PATHS.length; counter++)
    {
        File imageFile = new  File(PATHS[i]);
        if(imageFile.exists()) {
            ((ViewHolderItemDetail) viewHolder).imageBody.setImageBitmap(BitmapFactory.decodeFile(imageFile.getAbsolutePath()));
        }
    }

}

public void addItems(String[] paths){
    for (int i=0; i<paths.length;i++){
        this.PATHS[i] = paths[i];
        notifyItemChanged(i);
       // notifyDataSetChanged();
    }
}
private class ViewHolderItemDetail extends RecyclerView.ViewHolder{
    ImageView imageBody;

    ViewHolderItemDetail(View v) {
        super(v);
        imageBody = v.findViewById(R.id.imageBody);
    }
 }

thanks

Karam.esf
  • 29
  • 6

4 Answers4

0

Use this library : Picasso Library

And refer to this link to load image into ImageView (in your adapters BindViewHolder). It's easy : Picasso Use Case

EDIT A sample code

Picasso.get().load("your/url/or/file/path").into(imageView);

Edit 2 : You must not iterate inside adapter . The adapter is responsible with one ViewHolder , for only one image that you are going to load .

0

Stavro Xhardha has suggested a library to load images in to an ImageView (although my personal preference would be for Glide, https://github.com/bumptech/glide)

However, one of your issues is that onBindViewHolder isn't being called. I could be mistaken, but I would always set the adapter on the RecylerView prior to setting the data on said adapter. So instead of;

recyclerView = view.findViewById(R.id.recycler);
recyclerView.setLayoutManager(layoutManager);
adapter.addItems(paths);
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);

You would have;

recyclerView = view.findViewById(R.id.recycler);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
adapter.addItems(paths);
adapter.notifyDataSetChanged();
Daniel T-D
  • 665
  • 6
  • 8
0

you can use glide library or piccaso library visit https://github.com/bumptech/glide for glide visit https://github.com/square/picasso for piccaso library so now you shod just make your adapter very easily .

first you should make a parameter in your class for image; for example you make a Person Class;

so your class is :

public class Person{
String name ; 
String lastName;
String imgUrl;
//define setter, getter and constactive

}

in your adaper in bindview you just set this image in picasso , such as below code

    public void onBindViewHolder(final ViewHolder holder,final int position){
  Piccaso.with(contect).load(list.get(position).getImageUrl()).into(holder.imageview);
}
Reza Taghizadeh
  • 347
  • 5
  • 11
-1

In fragment, you can take context as getActivity(), so try to change code as:

adapter = new ImagesBodyAdapter(view.getContext());

to

adapter = new ImagesBodyAdapter(getActivity());

shkschneider
  • 17,833
  • 13
  • 59
  • 112
Rishav Singla
  • 485
  • 4
  • 10