Android:RecyclerView:未连接适配器;跳过布局 [英] Android: RecyclerView: No adapter attached; skipping layout

查看:93
本文介绍了Android:RecyclerView:未连接适配器;跳过布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当显示RecyclerView列表时,我不断收到错误消息"RecyclerView:未连接适配器;跳过布局" .我有3个标签,一个标签具有从SQLite数据库填充的RecyclerView列表.我没有崩溃,并且数据在视图中正确显示,但是我仍然收到此错误.

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.

我认为这只是一个警告,因为数据正确放置到位,但是当我尝试onClick时它不起作用,并且我确定它与该错误有关.

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.

这是我的片段:

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);

    }
}

我的适配器:

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);

        }
    }
}

预先感谢

更新: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>

推荐答案

为避免此错误,请避免将findViewById放入CreateView方法中,而应将其放入onViewCreated中.

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);

}

修改

您的适配器应该像:

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);

        }
    }
}

这篇关于Android:RecyclerView:未连接适配器;跳过布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆