0

Following this guide, I tried implementing RecycleView into Fragments. I got the error E/RecyclerView: No adapter attached; skipping layout The recycle view don't display anything

I tried
recyclerview No adapter attached; skipping layout
RecyclerView in Fragment : No adapter attached skipping layout
and many more.

My Code:

package com.example.celebrity_proper;


import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;


/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends Fragment {

    private RecyclerView choiceRecycleView2 = null;
    private ChoiceAdapter adapter = null;
    private ArrayList<Choice> choiceArrayList;

    public MainFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d("Go here", "41");
        View view =  inflater.inflate(R.layout.fragment_main, container, false);

        choiceRecycleView2 = view.findViewById(R.id.choiceRecycleView);
        choiceRecycleView2.setLayoutManager(new LinearLayoutManager(getActivity()));
        choiceArrayList = new ArrayList<>();
        adapter = new ChoiceAdapter(getActivity(), choiceArrayList);
        choiceRecycleView2.setAdapter(adapter);
        createListData();

        return view;
    }


    public void createListData() {
        Choice choice = new Choice("Adolf Hitler");
        choiceArrayList.add(choice);
        choice = new Choice("Kim Jong Un");
        Log.d("Item Count", String.valueOf(adapter.getItemCount()));
        choiceArrayList.add(choice);
        adapter.notifyDataSetChanged();
    }

}

class Choice {
    private String name;

    public Choice(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }
}

class ChoiceAdapter extends RecyclerView.Adapter<ChoiceViewHolder> {

    private Context context;

    private ArrayList<Choice> choices;
    public ChoiceAdapter(Context context, ArrayList<Choice> choices) {
        this.context = context;
        this.choices = choices;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ChoiceViewHolder holder, int position) {
        Choice choice = choices.get(position);
        holder.setDetails(choice);
    }

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

class ChoiceViewHolder extends RecyclerView.ViewHolder {
    private TextView choice_row_text;

    public ChoiceViewHolder(View view) {
        super(view);

        choice_row_text = view.findViewById(R.id.choice_row_text);
    }

    public void setDetails(Choice choice) {
        choice_row_text.setText(choice.getName());
    }
}
Md. Sabbir Ahmed
  • 850
  • 8
  • 22

3 Answers3

0

You are sending empty list to adapter constructor. Right Way


        choiceRecycleView2 = view.findViewById(R.id.choiceRecycleView);
        choiceRecycleView2.setLayoutManager(new LinearLayoutManager(getActivity()));
        choiceArrayList = new ArrayList<>();

        createListData();

        adapter = new ChoiceAdapter(getActivity(), choiceArrayList);
        choiceRecycleView2.setAdapter(adapter);
        adapter.notifyDataSetChanged();


    public void createListData() {
        Choice choice = new Choice("Adolf Hitler");
        choiceArrayList.add(choice);
        choice = new Choice("Kim Jong Un");
        Log.d("Item Count", String.valueOf(adapter.getItemCount()));
        choiceArrayList.add(choice);
    }



0

Give Orientation to LayoutManager.

Sample Code.

LinearLayoutManager manager = new LinearLayoutManager(this);
managersetOrientation(LinearLayoutManager.VERTICAL);
list.setLayoutManager(manager);
choiceRecycleView2.setAdapter( adapter );
Soham Pandya
  • 386
  • 1
  • 14
  • as you commented in other ans that your recyclerview is working without fragment then , do one thing check recycler view is showing or not. start with basic. first give static size to recyclerview and give its background color. and see its showing or not. – Soham Pandya Dec 20 '19 at 06:20
  • Yes it shows up in the preview. But it "skips" the layout altogether, it doesn't even run the ```onCreateView```. Hence the error ```E/RecyclerView: No adapter attached; skipping layout``` – Joey Setiawan Dec 20 '19 at 07:26
0

So apparently it is an issue with AMD processor. I can't prove it since I am kicked from that class, I finished that class 2 months ago