没有连接适配器;跳过布局recyclerview

图像不显示。在一个SO例子中说要检查适配器中的计数,但我认为它是正确的。 我在logcat的没有连接适配器;跳过布局recyclerview

No adapter attached; skipping layout recyclerview 

此收到此错误是代码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);

这是我的适配器

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

}

}

回答:

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

另外,创建适配器时不需要调用notifyDataSetChanged()

public void myMethod(ArrayList result) { 

mAdapter = new MyAdapter(result,Search.this);

mRecyclerView.setAdapter(mAdapter);

}

回答:

请删除mAdapter.notifyDataSetChanged()前设置适配器在以下代码行中。您可以在为RecyclerView设置适配器后执行此操作。

@Override 

public void myMethod(ArrayList result) {

mAdapter = new MyAdapter(result,Search.this);

mAdapter.notifyDataSetChanged();

mRecyclerView.setAdapter(mAdapter);

以上是 没有连接适配器;跳过布局recyclerview 的全部内容, 来源链接: utcz.com/qa/264627.html

回到顶部