Home > Software engineering >  new View.OnClickListener () greyed out ->" No Adapter Attached, skipping Layout" error
new View.OnClickListener () greyed out ->" No Adapter Attached, skipping Layout" error

Time:09-17

I copied code for a recycler View from a Youtube tutorial but it won't work out for me. The new View.OnClickListener() is greyed out, with Android Studio suggesting to replace out with lambda. This is the only difference from my code to the one from the tutorial... The App runs through, but doesn't show the Layout, as I get the error message E/RecyclerView: No adapter attached; skipping layout.

what can I do to fix this?

This is the Adapter class:

package com.example.yourfoodweek;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

public class MealsRecViewAdapter extends RecyclerView.Adapter<MealsRecViewAdapter.ViewHolder>{
    private static final String TAG = "MealsRecViewAdapter";

    private ArrayList <Food> meals = new ArrayList<>();
    private Context mContext;

    public MealsRecViewAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_meals_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Log.d(TAG, "onBindViewHolder: Called");
        holder.txtName.setText(meals.get(position).getName());
        Glide.with(mContext)
                .asBitmap()
                .load(meals.get(position).getImageUrl())
                .into(holder.imgFood);

        holder.parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick (View v){
                Toast.makeText(mContext, meals.get(position).getName()   "Selected", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return meals.size();
    }

    public void setMeals(ArrayList<Food> meals) {
        this.meals = meals;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        private CardView parent;
        private ImageView imgFood;
        private TextView txtName;
        public ViewHolder(@NonNull View itemView){
            super(itemView);
            parent = itemView.findViewById(R.id.parent);
            txtName = itemView.findViewById(R.id.txtMealName);
            imgFood = itemView.findViewById(R.id.imgFood);
        }
    }
}

CodePudding user response:

both problems are totally different. first one was suggestion means if you don't want to apply you can skip it. but i will not suggest to do that.

for example:

(without Lambda)

myButton.setOnClickListener(new View.OnClickListerner(){
    @Override
    void onClick(View v) {
        // do some stuff related to action    
    }
});

(with Lambda)

myButton.setOnClickListener(v -> {
   // do some stuff related to action
})

for other question

no layout manager attached:

if your RecyclerView doesn't have any LayoutManager attached to it then it will skip cause it will not know how to arrange/show items.

Some LayoutManagers:

  • LinearLayoutManager
  • GridLayoutManager
  • StaggeredGridLayoutManager

if you want to create your own LayoutManager you can do it by just extending class LayoutManager

how to use it:

....
LinearLayoutManager layoutManager = new LinearLayoutManager();
layoutManager.setOrientation = LinearLayoutManager.HORIZONTAL
myRecyclerViewObj.setLayoutManager(layoutManager);
....

no adapter attached

similar to no layout manager this will be shown when no adapter attached to recycler view for this you have to use setAdapter method to set your adapater

for example:

....
MealsRecViewAdapter adapter = new MealsRecViewAdapter(this);
myRecyclerViewObj.setAdapter(adapter);
....
  • Related