Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView﹕ No adapter attached; skipping layout

I have been reading the different answers here on stackoverflow and on this blog post and tried to implement their solutions but I am still getting the error:

RecyclerView﹕ No adapter attached; skipping layout

So I initialize my recycler view in onCreateView in my fragment like this:

public class ProfileFragment extends Fragment {

private ModelUser user;

private View view;
private RecyclerView gridView;
private ProfilePhotoRecyclerViewAdapter adapter;
private List<ModelAttachment> photoList = new ArrayList<ModelAttachment>();

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.fragment_profile, container, false);

    initializeRecyclerView();

    //Get extra info for user
    QueryAPI query = new QueryAPI();
    query.userExtra(user, new QueryAPI.ApiResponse<ModelUser>() {
        @Override
        public void onCompletion(ModelUser result) {
            user = result;
                photoList.clear();
                photoList.addAll(0,user.getPhotos());

                adapter.notifyDataSetChanged();

            }
        });

     return view;

}

}

The function initializeRecyclerView:

void initializeRecyclerView() {
        gridView = (RecyclerView) view.findViewById(R.id.grid_view);
        StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.HORIZONTAL );
        adapter = new ProfilePhotoRecyclerViewAdapter(getActivity(), photoList);
        gridView.setAdapter(adapter);
        gridView.setLayoutManager(gridLayoutManager);
        gridView.setHasFixedSize(true);
    }

The layout in the fragment:

 <android.support.v7.widget.RecyclerView
  android:id="@+id/grid_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

And the layout of an item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<com.android.volley.toolbox.NetworkImageView
    android:id="@+id/imageView"
    android:contentDescription="@string/image_description"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop"
    android:adjustViewBounds="true" />

 </LinearLayout>

What am I doing wrong?

EDIT: here is the adapter:

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

    private LayoutInflater inflater;
    private List<ModelAttachment> photos; //data
    private Activity listContext;
    private int listRowLayoutId;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();


    public ProfilePhotoRecyclerViewAdapter(Activity context, List<ModelAttachment> objs) {
        photos = objs;
        listContext = context;
        this.notifyDataSetChanged();
    }

    @Override
    public ProfilePhotoRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {

        View itemView = LayoutInflater.from(listContext).inflate(R.layout.profile_photogrid_item, parent, false);

        return new ProfilePhotoRecyclerViewAdapter.ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ProfilePhotoRecyclerViewAdapter.ViewHolder viewHolder, int i) {

        ModelAttachment photo = photos.get(i);
        viewHolder.imageView.setImageUrl(photo.getUrl(), imageLoader);
    }

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


    public static class ViewHolder extends RecyclerView.ViewHolder {

        ModelAttachment photo;
        public NetworkImageView imageView;

        public ViewHolder(View itemView) {
            super(itemView);
            this.imageView = (NetworkImageView) itemView.findViewById(R.id.imageView);
        }
    }
}
like image 413
Kali Aney Avatar asked Aug 19 '15 06:08

Kali Aney


2 Answers

I never had this issue and it was really hard to replicate for me. 😺

It is because you aren't setting empty adapter to RecyclerView first. The blog you give didn't exactly told how. Here is how I normally write.

My adapter constructor normally looks like this.

public ProfilePhotoRecyclerViewAdapter(Activity context) {
  photos = new ArrayList<>();
  listContext = context;
}

And write public (setter) method for the object array list.

public void setPhotos(List< ModelAttachment> photoList) {
  photos.clear();
  photos.addAll(photoList);
  this.notifyItemRangeInserted(0, photos.size() - 1);
}

And in your fragment, you don't have to initialise with photo array anymore.

adapter = new ProfilePhotoRecyclerViewAdapter(getActivity());

Just call the setter method from adapter inside your API response's onCompletion method.

@Override
public void onCompletion(ModelUser result) {
  photoList.clear();
  photoList.addAll(0,user.getPhotos());

  adapter.setPhotos(photoList);
}

I think this might solve. Feel free to ask and discuss.

Cheers,

SH

like image 63
Swan Avatar answered Nov 14 '22 07:11

Swan


make sure you have set LayoutManager to RecyclerView. Hope it will help :)

LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
list.setLayoutManager(llm);
list.setAdapter( adapter );
like image 44
Zubair Rehman Avatar answered Nov 14 '22 08:11

Zubair Rehman