From b62ea3bc918fe2c87daf92117b39db90d3a46ebc Mon Sep 17 00:00:00 2001 From: sebastian Date: Fri, 1 Nov 2024 10:37:57 +0100 Subject: [PATCH] Show Concerts --- .../core/notevault/data/MusicNoteDAO.java | 3 + .../notevault/ui/gallery/ConcertAdapter.java | 63 +++++++++++++++++++ .../notevault/ui/gallery/GalleryFragment.java | 30 +++++++++ app/src/main/res/layout/concert_item.xml | 13 ++++ app/src/main/res/layout/fragment_gallery.xml | 17 ++--- 5 files changed, 114 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/core/notevault/ui/gallery/ConcertAdapter.java create mode 100644 app/src/main/res/layout/concert_item.xml diff --git a/app/src/main/java/core/notevault/data/MusicNoteDAO.java b/app/src/main/java/core/notevault/data/MusicNoteDAO.java index 858fcf5..dc2d102 100644 --- a/app/src/main/java/core/notevault/data/MusicNoteDAO.java +++ b/app/src/main/java/core/notevault/data/MusicNoteDAO.java @@ -23,4 +23,7 @@ public interface MusicNoteDAO { @Insert void insertConcert(Concert concert); + + @Query("SELECT * FROM concerts") + List getAllConcerts(); } diff --git a/app/src/main/java/core/notevault/ui/gallery/ConcertAdapter.java b/app/src/main/java/core/notevault/ui/gallery/ConcertAdapter.java new file mode 100644 index 0000000..5525bfe --- /dev/null +++ b/app/src/main/java/core/notevault/ui/gallery/ConcertAdapter.java @@ -0,0 +1,63 @@ +package core.notevault.ui.gallery; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; +import core.notevault.R; +import core.notevault.data.Concert; +import core.notevault.ui.home.NoteSongAdapter; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; + +public class ConcertAdapter extends RecyclerView.Adapter { + + private final List concertList; + + public ConcertAdapter(List concertList) { + this.concertList = concertList; + } + + public ConcertAdapter() { + this.concertList = new ArrayList<>(); + } + + @Override + public void onBindViewHolder(@NonNull ConcertViewHolder holder, int position) { + holder.concertTitleView.setText(concertList.get(position).getTitle()); + } + + @Override + public int getItemCount() { + return concertList.size(); + } + + @NonNull + @Override + public ConcertViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.concert_item, parent, false); + return new ConcertViewHolder(view); + } + + public void updateConcerts(List concerts) { + this.concertList.clear(); + this.concertList.addAll(concerts); + notifyDataSetChanged(); + } + + public class ConcertViewHolder extends RecyclerView.ViewHolder { + TextView concertTitleView; + + public ConcertViewHolder(@NonNull @NotNull View itemView) { + super(itemView); + concertTitleView = itemView.findViewById(R.id.concert_title); + } + } + + +} diff --git a/app/src/main/java/core/notevault/ui/gallery/GalleryFragment.java b/app/src/main/java/core/notevault/ui/gallery/GalleryFragment.java index 32d9783..a78edc4 100644 --- a/app/src/main/java/core/notevault/ui/gallery/GalleryFragment.java +++ b/app/src/main/java/core/notevault/ui/gallery/GalleryFragment.java @@ -1,5 +1,6 @@ package core.notevault.ui.gallery; +import android.os.AsyncTask; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -8,14 +9,23 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.floatingactionbutton.FloatingActionButton; import core.notevault.R; +import core.notevault.data.Concert; +import core.notevault.data.MusicDatabase; +import core.notevault.data.MusicNote; +import core.notevault.data.MusicNoteDAO; import core.notevault.databinding.FragmentGalleryBinding; import core.notevault.ui.gallery.editor.ConcertEditorDialog; +import java.util.List; + public class GalleryFragment extends Fragment { private FragmentGalleryBinding binding; + private ConcertAdapter concertAdapter; public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -25,12 +35,32 @@ public class GalleryFragment extends Fragment { binding = FragmentGalleryBinding.inflate(inflater, container, false); View root = binding.getRoot(); + RecyclerView recyclerView = root.findViewById(R.id.concert_recycler_view); + concertAdapter = new ConcertAdapter(); + recyclerView.setAdapter(concertAdapter); + recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + new LoadConcerts().execute(); FloatingActionButton addConcertBtn = root.findViewById(R.id.add_concert_btn); addConcertBtn.setOnClickListener(v -> onCreateNewConcert()); return root; } + private class LoadConcerts extends AsyncTask> { + + @Override + protected List doInBackground(Void... voids) { + MusicDatabase db = MusicDatabase.getDatabase(getContext()); + MusicNoteDAO musicNoteDAO = db.musicNoteDao(); + return musicNoteDAO.getAllConcerts(); + } + + @Override + protected void onPostExecute(List concerts) { + concertAdapter.updateConcerts(concerts); // Aktualisiere den Adapter mit den Titeln + } + } + public void onCreateNewConcert() { ConcertEditorDialog concertEditorDialog = new ConcertEditorDialog(); concertEditorDialog.show(getParentFragmentManager(), ConcertEditorDialog.TAG); diff --git a/app/src/main/res/layout/concert_item.xml b/app/src/main/res/layout/concert_item.xml new file mode 100644 index 0000000..7f450a9 --- /dev/null +++ b/app/src/main/res/layout/concert_item.xml @@ -0,0 +1,13 @@ + + + + + diff --git a/app/src/main/res/layout/fragment_gallery.xml b/app/src/main/res/layout/fragment_gallery.xml index 895f4e0..5704324 100644 --- a/app/src/main/res/layout/fragment_gallery.xml +++ b/app/src/main/res/layout/fragment_gallery.xml @@ -7,19 +7,12 @@ android:layout_height="match_parent" tools:context=".ui.gallery.GalleryFragment"> - + android:layout_height="match_parent" + android:layout_above="@+id/add_concert_btn"/> +