0

the images are not displaying. in a SO example said to check the count in the adapter, but i think its correct. I am getting this error in the logcat

No adapter attached; skipping layout recyclerview

this is the code mainactivity

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

this is my adapter

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

}
Rami
  • 7,879
  • 12
  • 36
  • 66
Moudiz
  • 7,211
  • 22
  • 78
  • 156

2 Answers2

1

Please remove mAdapter.notifyDataSetChanged() before setting the Adapter in following lines of code. You can do it once adapter is set for RecyclerView.

  @Override
        public void myMethod(ArrayList result) {
            mAdapter = new MyAdapter(result,Search.this);
            mAdapter.notifyDataSetChanged();
            mRecyclerView.setAdapter(mAdapter);
Mahendra Chhimwal
  • 1,810
  • 5
  • 21
  • 33
  • I did what you said , and still got the error E/RecyclerView: No adapter attached; skipping layout – Moudiz Feb 13 '16 at 17:15
  • Ok tell me // viewHolder.txtHeader.setText(mDataset.get(position)); commented code in Adapter working? I mean is text is displaying correctly?As error No adapter attached; skipping layout recyclerview is ok as you are not setting adapter in onCreate() of activity of onCreateView() of fragment, you doing in background thread. So this warning is seems fine to me. – Mahendra Chhimwal Feb 13 '16 at 17:21
  • thanks for your support my mistake was I didnt add execute for okhhtp. , when I added it worked. thanks for your support – Moudiz Feb 13 '16 at 17:25
  • Oh..silly mistakes.. :P , sometimes happens . You are welcome. – Mahendra Chhimwal Feb 13 '16 at 17:27
1

Try to debug your code and check if myMethod() method is executed.

Also there is no need to call notifyDataSetChanged() when you create the adapter.

public void myMethod(ArrayList result) {
       mAdapter = new MyAdapter(result,Search.this);
       mRecyclerView.setAdapter(mAdapter);
}
Rami
  • 7,879
  • 12
  • 36
  • 66