I have a serious problem making an app. I wanted to make a notepad, I add the notes but I wanted the order of the entry of notes to be the other way around, that is, the last note was the one that was shown first, as it was in a ArrayList
notes so use this :
private Note getNote(int position) {
Collections.reverse(notes);
return notes.get(position);
}
and I change them well but now there is a problem when executing the app you enter a note well, you enter the second note and the first note takes the value of the second one (as if it had been repeated), you put the third note and it comes out how it should be example
* first note added = a --- is displayed on screen at
** second note added = b --- is displayed on screen b (first note), b (second note)
* third note added = c --- shown on screen at (first note) b (second note) c (third note)
I'm sending you the adapter code:
public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.NoteHolder> {
private Context context;
private ArrayList<Note> notes;
private NoteEventListener listener;
private boolean multiCheckMode = false;
public NotesAdapter(Context context, ArrayList<Note> notes) {
this.context = context;
this.notes = notes;
}
@Override
public NoteHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.note_layout, parent, false);
return new NoteHolder(v);
}
@Override
public void onBindViewHolder(NoteHolder holder, int position) {
int c=notes.size();
final Note note = getNote(position);
if (note != null) {
holder.noteText.setText(note.getNoteText());
holder.noteDate.setText(NoteUtils.dateFromLong(note.getNoteDate())); // init note click event
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onNoteClick(note);
}
});
// init note long click
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
listener.onNoteLongClick(note);
return false;
}
});
// check checkBox if note selected
if (multiCheckMode) {
holder.checkBox.setVisibility(View.VISIBLE); // show checkBox if multiMode on
holder.checkBox.setChecked(note.isChecked());
} else holder.checkBox.setVisibility(View.GONE); // hide checkBox if multiMode off
}
}
@Override
public int getItemCount() {
return notes.size();
}
private Note getNote(int position) {
Collections.reverse(notes);
return notes.get(position);
}
/**
* get All checked notes
*
* @return Array
*/
public List<Note> getCheckedNotes() {
List<Note> checkedNotes = new ArrayList<>();
for (Note n : this.notes) {
if (n.isChecked())
checkedNotes.add(n);
}
return checkedNotes;
}
class NoteHolder extends RecyclerView.ViewHolder {
TextView noteText, noteDate;
CheckBox checkBox;
public NoteHolder(View itemView) {
super(itemView);
noteDate = itemView.findViewById(R.id.note_date);
noteText = itemView.findViewById(R.id.note_text);
checkBox = itemView.findViewById(R.id.checkBox);
}
}
public void setListener(NoteEventListener listener) {
this.listener = listener;
}
public void setMultiCheckMode(boolean multiCheckMode) {
this.multiCheckMode = multiCheckMode;
notifyDataSetChanged();
}
}
(I add that the code is actually taken from a scrapbook tutorial and I'm still not entirely clear about many things, I'm just learning and if I suggest a book, channel or medium to continue studying I'd appreciate it a lot )