0

RecyclerView items are not getting created - I'm trying to add a note feature (with MVVM+Room+liveData) to my project.

The same code is fine when used in a new android studio project. implementing it in my existing project is the issue. (both are with androidX and have the same implementations).

Other recyclerViews in my app are working correctly (not with MVVM)

The recycler view adapter is not getting accessed - I used Log flags.

The Code source is from here: https://codinginflow.com/tutorials/android/room-viewmodel-livedata-recyclerview-mvvm/part-6-recyclerview-adapter

Note activity:

public class NoteActivity extends AppCompatActivity {

    private NoteViewModel noteViewModel;

    ArrayList<Reviews> reviewList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note);

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);

        final NoteAdapter adapter = new NoteAdapter();
        recyclerView.setAdapter(adapter);

        noteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class);
        noteViewModel.getAllNotes().observe(this, new Observer<List<Note>>() {
            @Override
            public void onChanged(@Nullable List<Note> notes) {
                adapter.setNotes(notes);
            }
        });
}

Note Adapter:


public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder> {

    private static final String TAG = "Debug "+NoteAdapter.class.getSimpleName();

    private List<Note> notes = new ArrayList<>();

    @NonNull
    @Override
    public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.note_item, parent, false);
        Log.e (TAG, "in onCreateViewHolder");
        return new NoteHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull NoteHolder holder, int position) {
        Note currentNote = notes.get(position);
        holder.textViewTitle.setText(currentNote.getTitle());
        holder.textViewDescription.setText(currentNote.getDescription());
        holder.textViewPriority.setText(String.valueOf(currentNote.getPriority()));
        Log.e (TAG, "in onBindViewHolder");
    }

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

    public void setNotes(List<Note> notes) {
        this.notes = notes;
        notifyDataSetChanged();

    }

    class NoteHolder extends RecyclerView.ViewHolder {
        private TextView textViewTitle;
        private TextView textViewDescription;
        private TextView textViewPriority;

        public NoteHolder(View itemView) {
            super(itemView);
            textViewTitle = itemView.findViewById(R.id.text_view_title);
            textViewDescription = itemView.findViewById(R.id.text_view_description);
            textViewPriority = itemView.findViewById(R.id.text_view_priority);
            Log.e (TAG, "in NoteHolder");
        }
    }
}

the set notes void is the only function getting accessed. (using Log Flags)

The entire code with MVVM classes works in a blank studio project.

What could be the problem??

in Log it get this error:

E/RecyclerView: No adapter attached; skipping layout

SO 80
  • 197
  • 2
  • 11

1 Answers1

0

The issue is resolved after logging in MVVM components.

I removed the application from my device and installed it again, so the Room database instance was recreated correctly. Apparently, The instance was empty (I don't know why).

btw, this log error is not part of the issue (you can get it when everything is okay) see previously answered question in the link below

E/RecyclerView: No adapter attached; skipping layout 

recyclerview No adapter attached; skipping layout

SO 80
  • 197
  • 2
  • 11