RecyclerView-: No adapter attached; skipping layout - android

I have been reading the different answers here on stackoverflow post and tried to implement their solutions but I am still getting the error
public class Appetizer extends AppCompatActivity {
private RecyclerView recyclerView;
private AdapterFood mAdapter;
recyclerView = (RecyclerView)findViewById(R.id.lvaptzr);
mAdapter = new AdapterFood(Appetizer.this,data);
recyclerView.setAdapter(mAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(Appetizer.this));
this is my Adapter class:
public class AdapterFood extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private LayoutInflater inflater;
List<DataFood> data = Collections.emptyList();
DataFood current;
int currentPos=0;
public AdapterFood(Context context, List<DataFood> data){
this.context=context;
inflater=LayoutInflater.from(context);
this.data=data;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.list_item,parent,false);
Myholder holder=new Myholder(view);
return holder;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Myholder myholder=(Myholder) holder;
DataFood current=data.get(position);
myholder.textFoodname.setText(current.foodName);
myholder.textPrice.setText("Rp. " + current.price);
myholder.textId.setText(current.foodId);
Glide.with(context).load("http://kelompokdua.hol.es/pic/" + current.foodImage)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(myholder.ivFood);
}
#Override
public int getItemCount() {
return data.size();
}
class Myholder extends RecyclerView.ViewHolder{
TextView textFoodname,textPrice,textId;
ImageView ivFood;
public Myholder(View itemView) {
super(itemView);
textFoodname=(TextView) itemView.findViewById(R.id.textFoodname);
ivFood=(ImageView)itemView.findViewById(R.id.ivFood);
textId=(TextView)itemView.findViewById(R.id.textid);
textPrice=(TextView) itemView.findViewById(R.id.textPrice);
}
}
}
i try any method, but i can't solve the probems. what i am doing wrong?

You should call setLayoutManager before setAdapter .
recyclerView = (RecyclerView)findViewById(R.id.lvaptzr);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(Appetizer.this);
recyclerView.setLayoutManager(mLayoutManager); // 1st
mAdapter= new AdapterFood(Appetizer.this,data);
recyclerView.setAdapter(mAdapter); // 2nd

Related

I have an error "E/RecyclerView : No adapter attached: skipping layout"

My application closes when I access the activity where my RecyclerView is, it tells me that the problem is in my adapter but being new in this the truth is that I do not know what the solution could be. I already looked for other answers with the same problem, but I still do not make it work
public class RecyclerViewAdapterCategoria extends RecyclerView.Adapter<RecyclerViewAdapterCategoria.MyViewHolder> {
private Context mContext;
private List<Categoria> mData;
private RequestOptions option;
public RecyclerViewAdapterCategoria(Context mContext, List<Categoria> mData) {
this.mContext = mContext;
this.mData = mData;
//Request option for Glide
option = new RequestOptions().centerCrop().placeholder(R.drawable.loading_shape).error(R.drawable.loading_shape);
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
View view;
LayoutInflater inflater = LayoutInflater.from(mContext);
view = inflater.inflate(R.layout.categoria_row_item, parent, false);
final MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tv_id.setText(mData.get(position).getId_categoria());
holder.tv_nombre.setText(mData.get(position).getNombre());
//Load image from Internet
Glide.with(mContext).load(mData.get(position).getImagen_url()).apply(option).into(holder.iv_imagen);
}
#Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView tv_id;
TextView tv_nombre;
ImageView iv_imagen;
LinearLayout view_container;
public MyViewHolder(View itemView){
super(itemView);
view_container = itemView.findViewById(R.id.container);
tv_id = itemView.findViewById(R.id.categoria_id);
tv_nombre = itemView.findViewById(R.id.categoria_nombre);
iv_imagen = itemView.findViewById(R.id.categoria_imagen);
}
}
}
I can't understand your problem because of language. But you title is E/RecyclerView: No adapter attached; skipping layout . So I think you missed to declare your Adapter in your yourRecyclerViewActivity class.
For basic you have to add
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this,yourList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(myAdapter );
to your recyclerViewActivity Class.
#vimukthi is correct and you're probably missing this code recyclerView.setAdapter(myAdapter); on the class that does the binding and call for the recyclerView.

RecyclerView onCreateViewHolder not called

RecyclerView setAdapter do but calling method ChatAdapter, registerAdapterDataObserver, onAttachedToRecyclerView, getItemCount 5 calling
xml layout_height is match_parent
getItemCount is 5 not null, but onCreateViewHolder is not called.
MyActivity.java:
List<ChatMessage> messages = new ArrayList<ChatMessage>();
LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
reyclerview_message_list.setAdapter(new ChatAdapter(messages, R.layout.chat_send));
reyclerview_message_list.setLayoutManager(new LinearLayoutManager(this));
reyclerview_message_list.setItemAnimator(new DefaultItemAnimator());
reyclerview_message_list.addItemDecoration(new DividerItemDecoration(getApplicationContext(), layoutManager.getOrientation()));
Adapter.java:
public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {
private List<ChatMessage> itemList;
private int itemLayout;
public ChatAdapter(List<ChatMessage> items, int itemLayout){
this.itemList = items;
this.itemLayout = itemLayout;
}
#Override
public ChatAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(itemLayout, viewGroup, false);
return new ChatAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(ChatAdapter.ViewHolder viewholder, int position) {
ChatMessage item = itemList.get(position);
viewholder.messageBody.setText(item.getMessageBody());
viewholder.messageTime.setText(item.getMessageTime());
viewholder.messageState.setText(item.getMessageState());
viewholder.itemView.setTag(item);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView messageBody, messageTime, messageState;
public ViewHolder(View itemView) {
super(itemView);
messageBody = (TextView)itemView.findViewById(R.id.text_message_body);
messageTime= (TextView)itemView.findViewById(R.id.text_message_time);
messageState= (TextView)itemView.findViewById(R.id.text_message_state);
}
}
#Override
public int getItemCount() {
return itemList.size();
}
Why is onCreateViewHolder not called?
I just update your Adapter code and its working well.
public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {
private List<ChatMessage> itemList;
private int itemLayout;
private Context mContext;
public ChatAdapter(List<ChatMessage> items, int itemLayout, Context context){
this.itemList = items;
this.itemLayout = itemLayout;
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(mContext).inflate(itemLayout, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder viewholder, int position) {
ChatMessage item = itemList.get(position);
viewholder.messageBody.setText(item.getMessageBody());
viewholder.messageTime.setText(item.getMessageTime());
viewholder.messageState.setText(item.getMessageState());
viewholder.itemView.setTag(item);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView messageBody, messageTime, messageState;
public ViewHolder(View itemView) {
super(itemView);
messageBody = (TextView)itemView.findViewById(R.id.text_message_body);
messageTime= (TextView)itemView.findViewById(R.id.text_message_time);
messageState= (TextView)itemView.findViewById(R.id.text_message_state);
}
}
#Override
public int getItemCount() {
return itemList.size();
}
}
And you Activity or Fragment first get the RecyclerView and then set adapter like this:
RecyclerView reyclerview_message_list = findViewById(R.id.recyclerView);
reyclerview_message_list.setAdapter(new ChatAdapter(messages, R.layout.list_row, this)); // passed another parameter Context
reyclerview_message_list.setLayoutManager(new LinearLayoutManager(this));
reyclerview_message_list.setItemAnimator(new DefaultItemAnimator());
reyclerview_message_list.addItemDecoration(new DividerItemDecoration(getApplicationContext(), layoutManager.getOrientation()));
Hope this will solve your problem.
You didn't notify your adapter after setting the value. While you setting the adapter messages is empty. When you adding the value in messages using addAll(message_objects) those data are added in ArrayList but you have to notify the adapter using notifyDataSetChanged(). Declare your adapter in class level then create object of adapter before setting the adapter in recyclerview and notify the adapter after you added objects in ArrayList.
private ChatAdapter chatAdapter;//declare it globally(class level)
chatAdapter = new ChatAdapter(messages, R.layout.chat_send);
reyclerview_message_list.setAdapter(chatAdapter);
reyclerview_message_list.setLayoutManager(new LinearLayoutManager(this));
reyclerview_message_list.setItemAnimator(new DefaultItemAnimator());
reyclerview_message_list.addItemDecoration(new DividerItemDecoration(getApplicationContext(), layoutManager.getOrientation()));
messages.addAll(ChatMessage_Objects_you_getting_from_web_service);
chatAdapter.notifyDataSetChanged();

How to use same adapter in two activity in different ways

Is it possible to call same adapter in two activity for different different work
Here is my RecyclerAdapter:
Only difference when calling from two activity is in this line: From Activity1.java it is:
holder.Name.setText(arrayList.get(position).getName());
And from Activity2.java
holder.Name.setText(arrayList.get(position).getVehicle());
All other code is same how can I use same adapter for different different work.
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private ArrayList<Contact> arrayList= new ArrayList<>();
public RecyclerAdapter(ArrayList<Contact> arrayList){
this.arrayList= arrayList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder (MyViewHolder holder, int position) {
holder.Name.setText(arrayList.get(position).getName());
int sync_status = arrayList.get(position).getSync_status();
if(sync_status== DbContact.SYNC_STATUS_OK){
holder.Sync_Status.setImageResource(R.drawable.success);
}
else {
holder.Sync_Status.setImageResource(R.drawable.stopwatch);
}
}
#Override
public int getItemCount() {
return arrayList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
ImageView Sync_Status;
TextView Name;
public MyViewHolder(View itemView) {
super(itemView);
Sync_Status=(ImageView)itemView.findViewById(R.id.imgSync);
Name=(TextView)itemView.findViewById(R.id.txtName);
}
}
}
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private ArrayList<Contact> arrayList= new ArrayList<>();
private int whichActivity;
public RecyclerAdapter(ArrayList<Contact> arrayList, int activity){
whichActivity = activity;
this.arrayList= arrayList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder (MyViewHolder holder, int position) {
if(whichActivity == 0) {
holder.Name.setText(arrayList.get(position).getName());
}
else {
holder.Name.setText(arrayList.get(position).getVehicle());
}
int sync_status = arrayList.get(position).getSync_status();
if(sync_status== DbContact.SYNC_STATUS_OK){
holder.Sync_Status.setImageResource(R.drawable.success);
}
else {
holder.Sync_Status.setImageResource(R.drawable.stopwatch);
}
}
#Override
public int getItemCount() {
return arrayList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
ImageView Sync_Status;
TextView Name;
public MyViewHolder(View itemView) {
super(itemView);
Sync_Status=(ImageView)itemView.findViewById(R.id.imgSync);
Name=(TextView)itemView.findViewById(R.id.txtName);
}
}
}
Try this code. Now when you create your RecyclerAdapter in Activity1 call new RecyclerAdapter(arrayList, 0) and when you create your RecyclerAdapter in Activity2 call new RecyclerAdapter(arrayList, 1). You are just passing a variable into the constructor so your adapter knows which activity it is in and can run through different logic depending on the activity.

RecyclerView don't work

I have created RecyclerView inside RecyclerView, but for some reason my pictures are not displayed in the internal. Help please!
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case TYPE_SUBSKRIBE_LIKE_AS:
SubscribeLikeAsHolder subscribeLikeAs = (SubscribeLikeAsHolder) holder;
subscribeLikeAs.recyclerViewImagines.setLayoutManager(new LinearLayoutManager(ctx, LinearLayoutManager.HORIZONTAL, false));
subscribeLikeAs.recyclerViewImagines.setAdapter(imageRecyclerViewAdapter);
break;
}
}
public class ImageRecyclerViewAdapter extends RecyclerView.Adapter<ImageRecyclerViewAdapter.ImagesViewHolder>{
private Context ctx;
private LayoutInflater lInflater;
private ArrayList<ItemsImage> images;
public void InForEntries (Context context, ArrayList<ItemsImage> items) {
ctx = context;
images = items;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public static class ImagesViewHolder extends RecyclerView.ViewHolder {
ImageView image;
ImagesViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.imageViewSmall);
}
}
#Override
public ImagesViewHolder onCreateViewHolder(ViewGroup viewGroup, int typeView) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_for_images, viewGroup, false);
return new ImagesViewHolder(view);
}
#Override
public void onBindViewHolder(ImageRecyclerViewAdapter.ImagesViewHolder holder, int position) {
// holder.image.setBackground(images.get(position).getDrawable());
holder.image.setBackground(ctx.getResources().getDrawable(R.drawable.image_1));
}
#Override
public int getItemCount() {
return images.size();
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
A common mistake I make is forgetting to set a LinearLayoutManager to the RecyclerView.
private LinearLayoutManager mLayoutManager;
RecyclerView mRecyclerView;
mLinearLayoutManager = new LinearLayoutManager(context);
mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);

Android Studio, RecyclerView crashes my app on start up

I am battling with the android Recyclerviewer. I followed most tutorials and also read posts here on StackOverflow, but i am still not coming right.
MainActivity
public class MainActivity extends ActionBarActivity {
RecyclerView myrecyclerView;
MyAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myrecyclerView = (RecyclerView)findViewById(R.id.myRecycleView);
adapter = new MyAdapter(MainActivity.this, getData());
final LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
myrecyclerView.setLayoutManager(layoutManager);
myrecyclerView.setAdapter(adapter);
myrecyclerView.setItemAnimator(new DefaultItemAnimator());
//
}
//the Dataset
public static List<MyDataModel> getData()
{
List<MyDataModel> myData = new ArrayList<>();
String[] postText = {"ibm", "apple", "google", "microsoft"};
int[] postImgUrl = {R.drawable.google, R.drawable.ibm, R.drawable.microsoft, R.drawable.apple};
for(int x=0; x<postText.length && x<postImgUrl.length; x++)
{
MyDataModel myModel = new MyDataModel();
myModel.postImgUrl = postImgUrl[x];
myModel.postText = postText[x];
myData.add(myModel);
}
return myData;
}
MyAdapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
List<MyDataModel> MyDataset;
private LayoutInflater inflater;
public MyAdapter(Context cont, List<MyDataModel> MyDataset)
{
inflater = LayoutInflater.from(cont);
}
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int ViewType)
{
//get ItemLayoutView from xml
View itemLayoutView = inflater.inflate(R.layout.my_items_row, parent, false);
//pass it to the ViewHolder
ViewHolder myViewHolder = new ViewHolder(itemLayoutView);
return myViewHolder;
}
public void onBindViewHolder(ViewHolder viewHolder, int position)
{
viewHolder.postText.setText(MyDataset.get(position).postText);
viewHolder.postImage.setImageResource(MyDataset.get(position).postImgUrl);
}
public int getItemCount()
{
return MyDataset.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder
{
public TextView postText;
public ImageView postImage;
public ViewHolder(View v)
{
super(v);
postText = (TextView)v.findViewById(R.id.myPostText);
postImage = (ImageView)v.findViewById(R.id.myPostImage);
}
}
}
My model Data
public class MyDataModel {
String postText;
int postImgUrl;
}
I am testing on real device through android studio, other apps i that i am also testing are fine.
Please help thank you.
You didn't initialize MyDataset inside MyAdapter class

Categories

Resources