List Inserted Notes
This commit is contained in:
parent
f90919fb77
commit
d3b94fdcb3
@ -42,6 +42,7 @@ dependencies {
|
||||
implementation(libs.navigation.fragment)
|
||||
implementation(libs.navigation.ui)
|
||||
implementation(libs.room.common)
|
||||
implementation("androidx.recyclerview:recyclerview:1.3.2")
|
||||
testImplementation(libs.junit)
|
||||
androidTestImplementation(libs.ext.junit)
|
||||
androidTestImplementation(libs.espresso.core)
|
||||
|
@ -2,6 +2,7 @@ package core.notevault.ui.home;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.provider.OpenableColumns;
|
||||
import android.util.Log;
|
||||
@ -14,13 +15,23 @@ import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
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.MusicDatabase;
|
||||
import core.notevault.data.MusicNote;
|
||||
import core.notevault.data.MusicNoteDAO;
|
||||
import core.notevault.databinding.FragmentHomeBinding;
|
||||
import core.notevault.ui.metadatadialog.MetaDataDialog;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class HomeFragment extends Fragment {
|
||||
|
||||
private NoteSongAdapter noteSongAdapter;
|
||||
|
||||
private static final int PICK_FILE_REQUEST_CODE = 1;
|
||||
private FragmentHomeBinding binding;
|
||||
private HomeViewModel homeViewModel;
|
||||
@ -36,9 +47,37 @@ public class HomeFragment extends Fragment {
|
||||
FloatingActionButton importBtn = root.findViewById(R.id.importMusicNotesBtn);
|
||||
importBtn.setOnClickListener(v -> openFileChooser());
|
||||
|
||||
RecyclerView recyclerView = root.findViewById(R.id.note_recycler_view);
|
||||
noteSongAdapter = new NoteSongAdapter();
|
||||
recyclerView.setAdapter(noteSongAdapter);
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
|
||||
new LoadSongTitlesTask().execute();
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
private class LoadSongTitlesTask extends AsyncTask<Void, Void, List<String>> {
|
||||
|
||||
@Override
|
||||
protected List<String> doInBackground(Void... voids) {
|
||||
MusicDatabase db = MusicDatabase.getDatabase(getContext());
|
||||
MusicNoteDAO musicNoteDAO = db.musicNoteDao();
|
||||
List<MusicNote> songs = musicNoteDAO.getAllNotes();
|
||||
|
||||
List<String> result = new ArrayList<>();
|
||||
for (MusicNote song : songs) {
|
||||
result.add(song.getTitle());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(List<String> songTitles) {
|
||||
noteSongAdapter.updateSongTitles(songTitles); // Aktualisiere den Adapter mit den Titeln
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
|
@ -4,19 +4,27 @@ import android.net.Uri;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
import core.notevault.MainActivity;
|
||||
import core.notevault.data.MusicDatabase;
|
||||
import core.notevault.ui.metadatadialog.MetaDataDialog;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class HomeViewModel extends ViewModel {
|
||||
|
||||
private final MutableLiveData<String> mText;
|
||||
private final MutableLiveData<List<String>> noteTitles;
|
||||
|
||||
public HomeViewModel() {
|
||||
mText = new MutableLiveData<>();
|
||||
mText.setValue("This is home fragment");
|
||||
noteTitles = new MutableLiveData<>(new ArrayList<>());
|
||||
}
|
||||
|
||||
public LiveData<String> getText() {
|
||||
return mText;
|
||||
public LiveData<List<String>> getNoteTitles() {
|
||||
return noteTitles;
|
||||
}
|
||||
|
||||
public void setNoteTitles(ArrayList<String> noteTitles) {
|
||||
this.noteTitles.setValue(noteTitles);
|
||||
}
|
||||
|
||||
public void addNote(Uri uri) {
|
||||
|
@ -0,0 +1,58 @@
|
||||
package core.notevault.ui.home;
|
||||
|
||||
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 java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class NoteSongAdapter extends RecyclerView.Adapter<NoteSongAdapter.NoteViewHolder> {
|
||||
|
||||
private final List<String> noteTitles;
|
||||
|
||||
public NoteSongAdapter(List<String> noteTitles) {
|
||||
this.noteTitles = noteTitles;
|
||||
}
|
||||
|
||||
public NoteSongAdapter() {
|
||||
this.noteTitles = new ArrayList<>();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.item_song, parent, false);
|
||||
return new NoteViewHolder(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull NoteViewHolder holder, int position) {
|
||||
holder.titleTextView.setText(noteTitles.get(position));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return noteTitles.size();
|
||||
}
|
||||
|
||||
public void updateSongTitles(List<String> songTitles) {
|
||||
this.noteTitles.clear();
|
||||
this.noteTitles.addAll(songTitles);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
static class NoteViewHolder extends RecyclerView.ViewHolder {
|
||||
TextView titleTextView;
|
||||
|
||||
NoteViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
titleTextView = itemView.findViewById(R.id.note_title);
|
||||
}
|
||||
}
|
||||
}
|
@ -7,19 +7,11 @@
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".ui.home.HomeFragment">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/text_home"
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/note_recycler_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:textAlignment="center"
|
||||
android:textSize="20sp"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintBottom_toBottomOf="parent"/>
|
||||
android:layout_height="match_parent"
|
||||
android:layout_above="@+id/importMusicNotesBtn"/>
|
||||
|
||||
<com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
android:src="@drawable/add_24dp_e8eaed_fill0_wght400_grad0_opsz24"
|
||||
@ -31,5 +23,6 @@
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
android:layout_marginBottom="16dp"
|
||||
app:backgroundTint="@color/teal_200"/>
|
||||
app:backgroundTint="@color/teal_200"
|
||||
android:contentDescription="Add Music Notes"/>
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
13
app/src/main/res/layout/item_song.xml
Normal file
13
app/src/main/res/layout/item_song.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:padding="16dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/note_title"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="18sp" />
|
||||
</LinearLayout>
|
Loading…
Reference in New Issue
Block a user