poprawiono obsługę adaptera do nowego layoutu
This commit is contained in:
parent
4ecdb6d4a5
commit
00cb876e01
@ -10,6 +10,7 @@ import androidx.recyclerview.widget.RecyclerView;
|
|||||||
import androidx.recyclerview.widget.RecyclerView.Adapter;
|
import androidx.recyclerview.widget.RecyclerView.Adapter;
|
||||||
|
|
||||||
import com.example.notatkon.R;
|
import com.example.notatkon.R;
|
||||||
|
import com.example.notatkon.database.NoteEntity;
|
||||||
|
|
||||||
import org.w3c.dom.Text;
|
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 */
|
/* Aby klasa była adapterem musi dziedziczyć po RecyclerView.Adapter oraz wskazywać na ViewHolder */
|
||||||
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.ViewHolder>{
|
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.ViewHolder>{
|
||||||
|
|
||||||
private ArrayList<Note> arrNotes = new ArrayList<>();
|
private ArrayList<NoteEntity> arrNotes = new ArrayList<>();
|
||||||
|
|
||||||
//obiekt listy notatek
|
//obiekt listy notatek
|
||||||
private RecyclerView noteRecyclerView;
|
//private RecyclerView noteRecyclerView;
|
||||||
|
|
||||||
//konstruktor
|
//konstruktor
|
||||||
public NoteAdapter(ArrayList<Note> Notes, RecyclerView notesRecyclerView) {
|
public NoteAdapter(ArrayList<NoteEntity> arrNotes) {
|
||||||
arrNotes = Notes;
|
this.arrNotes = arrNotes;
|
||||||
noteRecyclerView = notesRecyclerView;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//implementacja ViewHoldera
|
//implementacja ViewHoldera
|
||||||
public static class ViewHolder extends RecyclerView.ViewHolder {
|
public static class ViewHolder extends RecyclerView.ViewHolder {
|
||||||
public TextView noteTitle;
|
|
||||||
public TextView noteContent;
|
public TextView noteTitle, noteSubtitle, textDateTime;
|
||||||
|
|
||||||
public ViewHolder(@NonNull View itemView) {
|
public ViewHolder(@NonNull View itemView) {
|
||||||
super(itemView);
|
super(itemView);
|
||||||
noteTitle = (TextView) itemView.findViewById(R.id.note_title);
|
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
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||||
|
View view = LayoutInflater.from(parent.getContext()).inflate(
|
||||||
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_note, parent, false);
|
R.layout.view_note,
|
||||||
|
parent,
|
||||||
|
false
|
||||||
|
);
|
||||||
// utwórz nowy ViewHolder i usuń
|
// utwórz nowy ViewHolder i usuń
|
||||||
return new ViewHolder(view);
|
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)
|
///// Replace the contents of a view (invoked by the layout manager)
|
||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
||||||
Note note = arrNotes.get(position);
|
holder.setNote(arrNotes.get(position));
|
||||||
((ViewHolder) holder).noteTitle.setText(note.getNoteTitle());
|
|
||||||
((ViewHolder) holder).noteContent.setText(note.getNoteContent());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getItemCount() {
|
public int getItemCount() {
|
||||||
return arrNotes.size();
|
return arrNotes.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getItemViewType(int position) {
|
||||||
|
return super.getItemViewType(position);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user