问题
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();
}
}
回答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);
}
回答2:
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);
来源:https://stackoverflow.com/questions/35382552/no-adapter-attached-skipping-layout-recyclerview