dodano możliwość kliknięcia istniejącej notatki - pogląd pusty

This commit is contained in:
Naiki00 2021-02-17 18:49:44 +01:00
parent 171ffc5a7a
commit 826e025f9e
3 changed files with 51 additions and 12 deletions

View File

@ -3,6 +3,7 @@ package com.example.notatkon.adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
@ -10,6 +11,7 @@ import androidx.recyclerview.widget.RecyclerView;
import com.example.notatkon.R;
import com.example.notatkon.entities.NoteEntity;
import com.example.notatkon.listener.NoteListener;
import java.util.ArrayList;
import java.util.List;
@ -20,22 +22,28 @@ import java.util.List;
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.ViewHolder>{
private List<NoteEntity> listNotes;
private NoteListener noteListener;
//konstruktor
public NoteAdapter(List<NoteEntity> listNotes) {
public NoteAdapter(List<NoteEntity> listNotes, NoteListener noteListener) {
this.listNotes = listNotes;
this.noteListener = noteListener;
}
//implementacja ViewHoldera
public static class ViewHolder extends RecyclerView.ViewHolder {
static class ViewHolder extends RecyclerView.ViewHolder {
public TextView noteTitle, noteSubtitle, textDateTime;
TextView noteTitle, noteSubtitle, textDateTime;
LinearLayout viewNote;
public ViewHolder(@NonNull View itemView) {
ViewHolder(@NonNull View itemView) {
super(itemView);
noteTitle = (TextView) itemView.findViewById(R.id.note_title);
noteSubtitle = (TextView) itemView.findViewById(R.id.note_subtitle);
textDateTime = (TextView) itemView.findViewById(R.id.textDateTime);
viewNote = (LinearLayout) itemView.findViewById(R.id.viewNote);
}
// wpisz notatke
@ -53,19 +61,28 @@ public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.ViewHolder>{
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(
return new ViewHolder(
LayoutInflater.from(parent.getContext()).inflate(
R.layout.view_note,
parent,
false
)
);
// utwórz nowy ViewHolder i usuń
return new ViewHolder(view);
//ViewHolder(view);
}
///// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.setNote(listNotes.get(position));
holder.viewNote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
noteListener.onNoteClicked(listNotes.get(position), position);
}
});
}
@Override

View File

@ -0,0 +1,8 @@
package com.example.notatkon.listener;
import com.example.notatkon.entities.NoteEntity;
public interface NoteListener {
void onNoteClicked(NoteEntity noteEntity, int position);
}

View File

@ -16,6 +16,7 @@ import com.example.notatkon.R;
import com.example.notatkon.adapter.NoteAdapter;
import com.example.notatkon.entities.NoteEntity;
import com.example.notatkon.database.NoteRoomDatabase;
import com.example.notatkon.listener.NoteListener;
import com.example.notatkon.note.CreateNote;
import java.util.ArrayList;
@ -26,9 +27,12 @@ https://developer.android.com/guide/components/activities/activity-lifecycle
https://developer.android.com/training/basics/intents/result
*/
public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements NoteListener {
private static final int REQUEST_CODE_NEW_NOTE = 1;
public static final int REQUEST_CODE_NEW_NOTE = 1;
public static final int REQUEST_EDIT_NOTE = 2;
private int notePosition = -1;
private List<NoteEntity> noteEntityList;
private RecyclerView noteRecycler;
@ -60,12 +64,22 @@ public class MainActivity extends AppCompatActivity {
);
noteEntityList = new ArrayList<>();
//połącz Adapter z RecycleView
noteAdapter = new NoteAdapter(noteEntityList);
noteAdapter = new NoteAdapter(noteEntityList, this);
noteRecycler.setAdapter(noteAdapter);
getAllNotes();
}
@Override
public void onNoteClicked(NoteEntity noteEntity, int position) {
notePosition = position;
Intent intent = new Intent(getApplicationContext(), CreateNote.class);
intent.putExtra("update", true);
intent.putExtra("noteEntity", noteEntity);
startActivityForResult(intent, REQUEST_EDIT_NOTE);
}
//pobranie notatek z bazy i wyswietlanie na ekranie
private void getAllNotes() {