I keep getting the error "RecyclerView: No adapter attached; skipping layout" when the RecyclerView list is shown. I have 3 tabs and one tab has a RecyclerView list that is populated from SQLite database. I don't get any crashes and the data is shown correctly in the view but I still get this error.
I thought it was just a warning because the data is in place correctly but when I tried onClick it doesn't work and I'm sure it has something to do with this error.
I know this question has been asked a lot before but I checked most of the questions and none has worked for me.
This is my fragment:
public class RecentsFragment extends Fragment { DatabaseHelper helper; List<MyPojo> dbList; RecyclerView mRecyclerView; private MyGridAdapter mAdapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View view = inflater.inflate(R.layout.my_recents_list, container, false); mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView); mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2)); mRecyclerView.setHasFixedSize(true); return view; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); setHasOptionsMenu(true); dbList = new ArrayList<MyPojo>(); dbList = getCat(); // getCat returns array list from sqlite database mAdapter = new MyGridAdapter(dbList); mRecyclerView.setAdapter(mAdapter); } }
My adapter:
public class MyGridAdapter extends RecyclerView.Adapter<MyGridAdapter.ViewHolder> { static List<MyPojo> dbList; static Context context; MyGridAdapter(Context context, List<MyPojo> dbList){ this.dbList = new ArrayList<MyPojo>(); this.context = context; this.dbList = dbList; } MyGridAdapter(List<MyPojo> dbList){ this.dbList = new ArrayList<MyPojo>(); this.dbList = dbList; } @Override public MyGridAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate( R.layout.categories_item, null); ViewHolder viewHolder = new ViewHolder(itemLayoutView); return viewHolder; } @Override public void onBindViewHolder(final MyGridAdapter.ViewHolder holder, int position) { holder.title.setText(dbList.get(position).getCat()); } @Override public int getItemCount() { return dbList.size(); } public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public TextView title; public LinearLayout placeHolder; ImageView image; public ViewHolder(View itemLayoutView) { super(itemLayoutView); title = (TextView) itemLayoutView.findViewById(R.id.placeName); } @Override public void onClick(View v) { Intent intent = new Intent(context, DetailsActivity.class); Bundle extras = new Bundle(); extras.putInt("catid", dbList.get(getAdapterPosition()).getCatid()); intent.putExtras(extras); context.startActivity(intent); } } }
Thanks in advance
Update: my_recents_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".RecentsFragment"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f8f8f8" android:divider="@null" android:listSelector="@android:color/transparent"/> </LinearLayout>