Android: RecyclerView: No adapter attached; skipping layout

扶醉桌前 提交于 2020-01-04 06:26:07

问题


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>

回答1:


To avoid this error, avoid to do the findViewById into the CreateView method but instead do it into the onViewCreated.

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

    return inflater.inflate(R.layout.my_recents_list, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
    mRecyclerView.setHasFixedSize(true);

    setHasOptionsMenu(true);
    dbList = new ArrayList<MyPojo>();

    dbList = getCat(); // getCat returns array list from sqlite database

    mAdapter = new MyGridAdapter(getActivity(), dbList);
    mRecyclerView.setAdapter(mAdapter);

}

Edit

You Adapter should be like:

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

    private List<MyPojo> dbList;
    private Context context;

    MyGridAdapter(Context context, List<MyPojo> dbList) {
        this.dbList = new ArrayList<MyPojo>(dbList);
        this.context = context;
    }

    @Override
    public MyGridAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // Note that the inflate method takes the layout to inflate, the parent to measure 
        // the layout and the third parameters is false so only this view will handle events like click event
        View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.categories_item, parent, false);

        return new ViewHolder(itemLayoutView);
    }

    @Override
    public void onBindViewHolder(final MyGridAdapter.ViewHolder holder, final int position) {
        MyPojo myPojo = dbList.get(position);
        holder.title.setText(myPojo.getCat());
        //You can register the click listener to the textview (or the whole item if you put it into the holder)
        holder.title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, DetailsActivity.class);

            Bundle extras = new Bundle();
            extras.putInt("catid", dbList.get(position).getCatid());
            intent.putExtras(extras);

            context.startActivity(intent);
            }
        });
    }

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

    //The class is static to avoid leaks from a non-static nested class
    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView title;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);
            title = (TextView) itemLayoutView.findViewById(R.id.placeName);

        }
    }
}


来源:https://stackoverflow.com/questions/35583686/android-recyclerview-no-adapter-attached-skipping-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!