Answer the question
In order to leave comments, you need to log in
What is the error in displaying the RecyclerView widget?
The code that should display the RecyclerView with data:
public class NotesListFragment extends Fragment {
public NotesListFragment() {
}
private RecyclerView mNoteRecyclerView;
private NoteAdapter mNoteAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NotesBase note = NotesBase.get(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.notes_list_fragment, container, false);
mNoteRecyclerView = (RecyclerView) view.findViewById(R.id.note_recycler_view);
mNoteRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
updateUI();
return view;
}
private class NoteHolder extends RecyclerView.ViewHolder {
private TextView mTitleNoteTextView;
private TextView mDateNoteTextView;
public NoteHolder(View itemView) {
super(itemView);
mTitleNoteTextView = (TextView) itemView.findViewById(R.id.ItemNoteTitle);
mDateNoteTextView = (TextView) itemView.findViewById(R.id.ItemNoteDate);
}
public void bindNote (Note note) {
Note mNote = note;
mTitleNoteTextView.setText(mNote.getmTitle_n());
mDateNoteTextView.setText(mNote.getmDate_n().toString());
}
}
private class NoteAdapter extends RecyclerView.Adapter<NoteHolder> {
private List<Note> mNotes;
public NoteAdapter(List<Note> notes) {
mNotes = notes;
}
public NoteHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View view = layoutInflater.inflate(R.layout.list_item, parent, false);
return new NoteHolder(view);
}
public void onBindViewHolder(NoteHolder holder, int position) {
Note note = mNotes.get(position);
holder.bindNote(note);
}
public int getItemCount() {
return mNotes.size();
}
}
private void updateUI() {
NotesBase notesBase = NotesBase.get(getActivity());
List<Note> notes = notesBase.getNotes();
mNoteAdapter = new NoteAdapter(notes);
mNoteRecyclerView.setAdapter(mNoteAdapter);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/note_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question