naprawiono wyświetlanie notatki wg kolejności dodawania na głównym ekranie

This commit is contained in:
Naiki00 2021-02-16 15:30:03 +01:00
parent 521c796075
commit 171ffc5a7a
7 changed files with 37 additions and 89 deletions

View File

@ -14,12 +14,15 @@ import java.util.List;
@Dao @Dao
public interface NoteDao { public interface NoteDao {
@Query("SELECT * FROM note_table") @Query("SELECT * FROM note_table ORDER BY id DESC")
List<NoteEntity> getNotes(); List<NoteEntity> getNotes();
@Insert(onConflict = OnConflictStrategy.IGNORE) @Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(NoteEntity noteEntity); void insert(NoteEntity noteEntity);
@Delete() @Delete()
void delete(NoteEntity noteEntity); void delete(NoteEntity noteEntity);
@Query("DELETE FROM note_Table")
void deleteAll();
} }

View File

@ -38,6 +38,7 @@ public class CreateNote extends AppCompatActivity {
imageBack.setOnClickListener(new View.OnClickListener() { imageBack.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
onBackPressed(); onBackPressed();
} }
}); });
@ -88,7 +89,6 @@ public class CreateNote extends AppCompatActivity {
finish(); finish();
} }
} }
new SaveNoteTask().execute(); new SaveNoteTask().execute();
} }

View File

@ -1,5 +1,6 @@
package com.example.notatkon.note; package com.example.notatkon.note;
import android.annotation.SuppressLint;
import android.content.Intent; import android.content.Intent;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
@ -20,6 +21,11 @@ import com.example.notatkon.note.CreateNote;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/*
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 {
private static final int REQUEST_CODE_NEW_NOTE = 1; private static final int REQUEST_CODE_NEW_NOTE = 1;
@ -41,47 +47,18 @@ public class MainActivity extends AppCompatActivity {
); );
}); });
//Toolbar toolbar = findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
/*
https://developer.android.com/guide/components/activities/activity-lifecycle
https://developer.android.com/training/basics/intents/result
*/
/* wersja z fab
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivityForResult(intent, REQUEST_CODE_NEW_NOTE);
//Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
// .setAction("Action", null).show();
}
});
*/
//-- Pierwsza wersja apki -- //-- Pierwsza wersja apki --
//metody //metody
//https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView#next-steps //https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView#next-steps
noteRecycler = (RecyclerView) findViewById(R.id.notes); noteRecycler = findViewById(R.id.notes);
//ustaw LayoutManagera wertykalnie //ustaw LayoutManagera wertykalnie
//recyclerView.setLayoutManager(new LinearLayoutManager(this)); //recyclerView.setLayoutManager(new LinearLayoutManager(this));
//ustaw LayoutManagera horyzontalnie //ustaw LayoutManagera horyzontalnie
noteRecycler.setLayoutManager( noteRecycler.setLayoutManager(
new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL) new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
); );
//wczytaj listę z klasy Note oraz dodaj obiekt jej klasy
noteEntityList = new ArrayList<>(); noteEntityList = new ArrayList<>();
//połącz Adapter z RecycleView //połącz Adapter z RecycleView
noteAdapter = new NoteAdapter(noteEntityList); noteAdapter = new NoteAdapter(noteEntityList);
noteRecycler.setAdapter(noteAdapter); noteRecycler.setAdapter(noteAdapter);
@ -89,40 +66,10 @@ public class MainActivity extends AppCompatActivity {
getAllNotes(); getAllNotes();
} }
/*
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
*/
//pobranie notatek z bazy i wyswietlanie na ekranie //pobranie notatek z bazy i wyswietlanie na ekranie
private void getAllNotes() { private void getAllNotes() {
@SuppressLint("StaticFieldLeak")
class GetNotesTask extends AsyncTask<Void, Void, List<NoteEntity>> { class GetNotesTask extends AsyncTask<Void, Void, List<NoteEntity>> {
@Override @Override
@ -136,7 +83,7 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
protected void onPostExecute(List<NoteEntity> noteEntities) { protected void onPostExecute(List<NoteEntity> noteEntities) {
super.onPostExecute(noteEntities); super.onPostExecute(noteEntities);
Log.d("NOTES", noteEntities.toString()); //Log.d("NOTES", noteEntities.toString());
if (noteEntityList.size() == 0) { if (noteEntityList.size() == 0) {
noteEntityList.addAll(noteEntities); noteEntityList.addAll(noteEntities);
noteAdapter.notifyDataSetChanged(); noteAdapter.notifyDataSetChanged();
@ -153,7 +100,7 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_NEW_NOTE && requestCode == RESULT_OK) { if (requestCode == REQUEST_CODE_NEW_NOTE && resultCode == RESULT_OK) {
getAllNotes(); getAllNotes();
} }
} }

View File

@ -3,6 +3,6 @@
android:shape="rectangle"> android:shape="rectangle">
<solid android:color="@color/colorNoteColor" /> <solid android:color="@color/colorNoteColor" />
<corners android:radius="@dimen/_25sdp" /> <corners android:radius="@dimen/_12sdp" />
</shape> </shape>

View File

@ -5,7 +5,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="#f7f7f7" android:background="#f7f7f7"
tools:context=".MainActivity"> tools:context=".note.MainActivity">
<!-- stary layout z górnym toolbarem <!-- stary layout z górnym toolbarem
<com.google.android.material.appbar.AppBarLayout <com.google.android.material.appbar.AppBarLayout
@ -98,7 +98,7 @@
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/notes" android:id="@+id/notes"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="0dp"
android:layout_marginStart="@dimen/_2sdp" android:layout_marginStart="@dimen/_2sdp"
android:layout_marginEnd="@dimen/_2sdp" android:layout_marginEnd="@dimen/_2sdp"
android:clipToPadding="false" android:clipToPadding="false"

View File

@ -5,17 +5,19 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@color/colorPrimary" android:background="@color/colorPrimary"
tools:context=".CreateNote"> tools:context=".note.CreateNote">
<ScrollView <ScrollView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginBottom="@dimen/_40sdp" android:layout_marginBottom="@dimen/_40sdp"
android:clipToPadding="false"
android:paddingBottom="@dimen/_10sdp"> android:paddingBottom="@dimen/_10sdp">
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content"
android:animateLayoutChanges="true">
<ImageView <ImageView
android:id="@+id/back" android:id="@+id/back"
@ -53,6 +55,7 @@
android:textSize="@dimen/_16ssp" android:textSize="@dimen/_16ssp"
android:inputType="text" android:inputType="text"
android:hint="@string/note_title" android:hint="@string/note_title"
android:imeOptions="actionDone"
app:layout_constraintTop_toBottomOf="@id/back" /> app:layout_constraintTop_toBottomOf="@id/back" />
<TextView <TextView
@ -104,6 +107,7 @@
android:layout_marginTop="@dimen/_15sdp" android:layout_marginTop="@dimen/_15sdp"
android:layout_marginEnd="@dimen/_12sdp" android:layout_marginEnd="@dimen/_12sdp"
android:layout_marginBottom="@dimen/_15sdp" android:layout_marginBottom="@dimen/_15sdp"
android:gravity="top"
android:hint="@string/note_content" android:hint="@string/note_content"
android:inputType="textMultiLine" android:inputType="textMultiLine"
android:textColor="@color/white" android:textColor="@color/white"

View File

@ -4,51 +4,45 @@
android:id="@+id/viewNote" android:id="@+id/viewNote"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="5dp" android:background="@drawable/background_note"
android:backgroundTint="@color/colorAccent" android:layout_marginStart="@dimen/_14sdp"
app:cardCornerRadius="10dp" android:layout_marginTop="@dimen/_14sdp"
app:cardElevation="2dp"
app:contentPadding="0dp" app:contentPadding="0dp"
android:orientation="vertical" android:orientation="vertical">
android:padding="2dp">
<TextView <TextView
android:id="@+id/note_title" android:id="@+id/note_title"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_8sdp" android:layout_marginStart="@dimen/_8sdp"
android:layout_marginTop="@dimen/_8sdp" android:layout_marginTop="@dimen/_8sdp"
android:layout_marginEnd="@dimen/_8sdp" android:layout_marginEnd="@dimen/_8sdp"
android:text="note_title"
android:textAlignment="viewStart" android:textAlignment="viewStart"
android:textSize="@dimen/_14ssp" android:textSize="@dimen/_14ssp"
android:textStyle="bold" /> android:textStyle="bold"
android:textColor="@color/black" />
<TextView <TextView
android:id="@+id/note_subtitle" android:id="@+id/note_subtitle"
android:text="note_subtitle"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_8sdp" android:layout_marginStart="@dimen/_8sdp"
android:layout_marginTop="@dimen/_4sdp" android:layout_marginTop="@dimen/_4sdp"
android:layout_marginEnd="@dimen/_8sdp" android:layout_marginEnd="@dimen/_8sdp"
android:layout_marginBottom="@dimen/_4sdp" android:layout_marginBottom="@dimen/_4sdp"
android:textSize="@dimen/_12ssp"
android:includeFontPadding="false"
android:textColor="@color/black"
android:textStyle="normal" /> android:textStyle="normal" />
<TextView <TextView
android:id="@+id/textDateTime" android:id="@+id/textDateTime"
android:text="note_date"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textAlignment="viewStart" android:textSize="@dimen/_10ssp"
android:textSize="14sp"
android:maxLines="4"
android:ellipsize="end"
android:layout_marginStart="@dimen/_8sdp" android:layout_marginStart="@dimen/_8sdp"
android:layout_marginEnd="@dimen/_8sdp" android:layout_marginEnd="@dimen/_8sdp"
android:layout_marginBottom="@dimen/_8sdp" android:layout_marginBottom="@dimen/_8sdp"
app:layout_constraintBottom_toBottomOf="parent" android:textColor="@color/black" />
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout> </LinearLayout>