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
public interface NoteDao {
@Query("SELECT * FROM note_table")
@Query("SELECT * FROM note_table ORDER BY id DESC")
List<NoteEntity> getNotes();
@Insert(onConflict = OnConflictStrategy.IGNORE)
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(NoteEntity noteEntity);
@Delete()
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() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
@ -88,7 +89,6 @@ public class CreateNote extends AppCompatActivity {
finish();
}
}
new SaveNoteTask().execute();
}

View File

@ -1,5 +1,6 @@
package com.example.notatkon.note;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
@ -20,6 +21,11 @@ import com.example.notatkon.note.CreateNote;
import java.util.ArrayList;
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 {
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 --
//metody
//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
//recyclerView.setLayoutManager(new LinearLayoutManager(this));
//ustaw LayoutManagera horyzontalnie
noteRecycler.setLayoutManager(
new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
);
//wczytaj listę z klasy Note oraz dodaj obiekt jej klasy
noteEntityList = new ArrayList<>();
//połącz Adapter z RecycleView
noteAdapter = new NoteAdapter(noteEntityList);
noteRecycler.setAdapter(noteAdapter);
@ -89,40 +66,10 @@ public class MainActivity extends AppCompatActivity {
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
private void getAllNotes() {
@SuppressLint("StaticFieldLeak")
class GetNotesTask extends AsyncTask<Void, Void, List<NoteEntity>> {
@Override
@ -136,7 +83,7 @@ public class MainActivity extends AppCompatActivity {
@Override
protected void onPostExecute(List<NoteEntity> noteEntities) {
super.onPostExecute(noteEntities);
Log.d("NOTES", noteEntities.toString());
//Log.d("NOTES", noteEntities.toString());
if (noteEntityList.size() == 0) {
noteEntityList.addAll(noteEntities);
noteAdapter.notifyDataSetChanged();
@ -153,7 +100,7 @@ public class MainActivity extends AppCompatActivity {
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent 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();
}
}

View File

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

View File

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

View File

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

View File

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