没有适配器;跳过布局recyclerview

时间:2016-02-13 16:48:00

标签: android android-adapter android-recyclerview

图像未显示。在一个SO示例中说要检查适配器中的计数,但我认为它是正确的。 我在logcat中收到此错误

No adapter attached; skipping layout recyclerview

这是代码主要活动

RecyclerOkHttpHandler handler = new RecyclerOkHttpHandler( this, new RecyclerOkHttpHandler.MyInterface() {
        @Override
        public void myMethod(ArrayList result) {
            mAdapter = new MyAdapter(result,Search.this);
            mAdapter.notifyDataSetChanged();
            mRecyclerView.setAdapter(mAdapter);

这是我的适配器

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private ArrayList<Listitem> mDataset;
    Context mContext;
    public class ImageViewHolder extends RecyclerView.ViewHolder {
        //ImageView mImage;
        public TextView txtHeader;
        public TextView txtFooter;
        public ImageView image;
        public ImageViewHolder(View itemView) {
            super (itemView);
            txtHeader = (TextView) itemView.findViewById(R.id.firstLine);
            txtFooter = (TextView) itemView.findViewById(R.id.secondLine);
             image = (ImageView) itemView.findViewById(R.id.icon);
        }
    }


    public void add(int position, Listitem item) { //changed from string to listitem

        mDataset.add(position, item);
        notifyItemInserted(position);
    }

    public void remove(String item) {
        int position = mDataset.indexOf(item);
        mDataset.remove(position);
        notifyItemRemoved(position);
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(ArrayList<Listitem> myDataset, Context context) {

        mDataset = myDataset;
        mContext = context;

    }

    // Create new views (invoked by the layout manager)
    @Override
    public RecyclerView.ViewHolder  onCreateViewHolder(ViewGroup parent,
                                                       int viewType) {

            // create a new view
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);
            // set the view's size, margins, paddings and layout parameters
            ImageViewHolder vh = new ImageViewHolder(v);
            return vh;
        }


    private static final int TYPE_IMAGE = 1;
    private static final int TYPE_GROUP = 2;

    @Override
    public int getItemViewType(int position) {
        // here your custom logic to choose the view type
        return position;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder TextViewHolder, int position) {

                ImageViewHolder viewHolder = (ImageViewHolder) TextViewHolder;
                //  viewHolder.txtHeader.setText(...)
        final Listitem item;

      //  final String name = mDataset.get(position);
        item = mDataset.get(position);
      //  viewHolder.txtHeader.setText(mDataset.get(position));
        viewHolder.txtHeader.setText(mDataset.get(position).getUrl());


        Picasso.with(mContext)
                .load(item.getUrl())
                .placeholder(R.drawable.logo)
                .fit()
                .noFade()
                .into(viewHolder.image);

              /*  viewHolder.txtFooter.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        remove(item);
                    }
                });*/

               // viewHolder.txtFooter.setText("Footer: " + mDataset.get(position));

        }


    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.size();
    }

}

2 个答案:

答案 0 :(得分:1)

请在以下代码行中设置适配器之前删除 mAdapter.notifyDataSetChanged()。一旦为RecyclerView设置了适配器,就可以执行此操作。

  @Override
        public void myMethod(ArrayList result) {
            mAdapter = new MyAdapter(result,Search.this);
            mAdapter.notifyDataSetChanged();
            mRecyclerView.setAdapter(mAdapter);

答案 1 :(得分:1)

尝试调试代码并检查是否执行了myMethod()方法。

创建适配器时也无需调用notifyDataSetChanged()

public void myMethod(ArrayList result) {
       mAdapter = new MyAdapter(result,Search.this);
       mRecyclerView.setAdapter(mAdapter);
}