poprawiono obsługę adaptera do nowego layoutu

This commit is contained in:
Naiki00 2021-02-15 23:19:41 +01:00
parent 4ecdb6d4a5
commit 00cb876e01

View File

@ -10,6 +10,7 @@ import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import com.example.notatkon.R;
import com.example.notatkon.database.NoteEntity;
import org.w3c.dom.Text;
@ -20,34 +21,48 @@ import java.util.ArrayList;
/* Aby klasa była adapterem musi dziedziczyć po RecyclerView.Adapter oraz wskazywać na ViewHolder */
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.ViewHolder>{
private ArrayList<Note> arrNotes = new ArrayList<>();
private ArrayList<NoteEntity> arrNotes = new ArrayList<>();
//obiekt listy notatek
private RecyclerView noteRecyclerView;
//private RecyclerView noteRecyclerView;
//konstruktor
public NoteAdapter(ArrayList<Note> Notes, RecyclerView notesRecyclerView) {
arrNotes = Notes;
noteRecyclerView = notesRecyclerView;
public NoteAdapter(ArrayList<NoteEntity> arrNotes) {
this.arrNotes = arrNotes;
}
//implementacja ViewHoldera
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView noteTitle;
public TextView noteContent;
public TextView noteTitle, noteSubtitle, textDateTime;
public ViewHolder(@NonNull View itemView) {
super(itemView);
noteTitle = (TextView) itemView.findViewById(R.id.note_title);
noteContent = (TextView) itemView.findViewById(R.id.note_content);
noteSubtitle = (TextView) itemView.findViewById(R.id.note_subtitle);
textDateTime = (TextView) itemView.findViewById(R.id.textDateTime);
}
// wpisz notatke
void setNote(NoteEntity note) {
noteTitle.setText(note.getTitle());
if (note.getSubtitle().trim().isEmpty()) {
noteSubtitle.setVisibility(View.GONE);
} else {
noteSubtitle.setText(note.getSubtitle());
}
textDateTime.setText(note.getDateTime());
}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_note, parent, false);
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.view_note,
parent,
false
);
// utwórz nowy ViewHolder i usuń
return new ViewHolder(view);
}
@ -55,13 +70,16 @@ public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.ViewHolder>{
///// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Note note = arrNotes.get(position);
((ViewHolder) holder).noteTitle.setText(note.getNoteTitle());
((ViewHolder) holder).noteContent.setText(note.getNoteContent());
holder.setNote(arrNotes.get(position));
}
@Override
public int getItemCount() {
return arrNotes.size();
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
}