Home » Android » I'm getting the correct uri but unable to load image into recyclerview?

I'm getting the correct uri but unable to load image into recyclerview?

Posted by: admin November 1, 2017 Leave a comment

Questions:

I’m getting ArrayList of Uri in my Fragment Class and that’s also been sent to the adapter class properly but image is not shown on the recyclerView after checking log cat I’m getting following error.I’m using Featured RecyclerView (a github libraray).

Error:

E/RecyclerView: No adapter attached; skipping layout

Code of onCreateViewHolder():

recyclerView =  v.findViewById(R.id.featured_recycler_view_society);
    FeatureLinearLayoutManager layoutManager = new FeatureLinearLayoutManager(getActivity().getApplicationContext());

           recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(layoutManager);
  adapter = new CustomRecyclerViewAdapter(getActivity().getApplicationContext  (),selected);
    recyclerView.setAdapter(adapter);

OnActivityResult code:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constants.REQUEST_CODE && resultCode == RESULT_OK && data != null) {
    //The array list has the image paths of the selected images
    ArrayList<Image> images = data.getParcelableArrayListExtra(Constants.INTENT_EXTRA_IMAGES);
    ArrayList<Uri> selected=new ArrayList<>();
    for (int i = 0, l = images.size(); i < l; i++) {
        selected.add(Uri.parse(images.get(i).path));
    }
  adapter.notifyDataSetChanged();
                Toast.makeText(getActivity().getApplicationContext(),selected.toString(),Toast.LENGTH_SHORT).show();
                      }
}

Adapter Code:

public class CustomRecyclerViewAdapter extends FeatureRecyclerViewAdapter<CustomRecyclerViewAdapter.CustomRecyclerViewHolder> {

private Context context;
private ArrayList<Uri> image;

public CustomRecyclerViewAdapter(Context context,ArrayList<Uri> image) {
    this.context = context;
    this.image=image;

}

@Override
public CustomRecyclerViewHolder onCreateFeaturedViewHolder(ViewGroup parent, int viewType) {
    return new CustomRecyclerViewHolder(
            LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.timeline_holder, parent, false));
}

@Override
public void onBindFeaturedViewHolder(final CustomRecyclerViewHolder holder, final int position) {
           try {


            link="file://"+image.get(position).toString();
            Toast.makeText(context,link, Toast.LENGTH_SHORT).show();
        }
       Glide.with(context)
                .load(link).into(holder.ivBackground);
              } catch (Exception e) {
        e.printStackTrace();
    }




}

@Override
public int getFeaturedItemsCount() {
    if(image==null){
        return 0;
    }
    return image.size();
}

@Override
public int getItemViewType(int position) {
    return position;
}

@Override
public void onSmallItemResize(CustomRecyclerViewHolder holder, int position, float offset) {

}

@Override
public void onBigItemResize(CustomRecyclerViewHolder holder, int position, float offset) {
       }

public static class CustomRecyclerViewHolder extends RecyclerView.ViewHolder {
    ImageView ivBackground;

    public CustomRecyclerViewHolder(View itemView) {
        super(itemView);

        ivBackground = itemView.findViewById(R.id.iv_background);
                }
}

}

Answers:

You should attach the adapter in your main thread and not in some callback like onActivityResult(..). You can just update the adapter from callbacks by calling adapter.notifyDataSetChanged().

UPDATED

onCreateView()

recyclerView =  v.findViewById(R.id.featured_recycler_view_society);
FeatureLinearLayoutManager layoutManager = new FeatureLinearLayoutManager(getActivity().getApplicationContext());

       recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
selected=new ArrayList();
adapter = new CustomRecyclerViewAdapter(getActivity().getApplicationContext  (),selected);
recyclerView.setAdapter(adapter);

onActivityResult()

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == Constants.REQUEST_CODE && resultCode == RESULT_OK && data != null) {
        //The array list has the image paths of the selected images
        ArrayList<Image> images = data.getParcelableArrayListExtra(Constants.INTENT_EXTRA_IMAGES);
        for (int i = 0, l = images.size(); i < l; i++) {
            selected.add(Uri.parse(images.get(i).path));
        }
      adapter.notifyDataSetChanged();
                    Toast.makeText(getActivity().getApplicationContext(),selected.toString(),Toast.LENGTH_SHORT).show();
                          }
    }