Commit bdfadfbc authored by 9731301's avatar 9731301

get data in activity andset it in fragment with bundle

parent 69310e71
package com.example.mynotepad.MenuFeatures.AllNotes; package com.example.mynotepad.MenuFeatures.AllNotes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
...@@ -10,24 +8,18 @@ import androidx.recyclerview.widget.RecyclerView; ...@@ -10,24 +8,18 @@ import androidx.recyclerview.widget.RecyclerView;
import androidx.room.Room; import androidx.room.Room;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.util.Log;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import com.example.mynotepad.MenuFeatures.AllNotes.MyNoteRecyclerView.MyAdaptor;
import com.example.mynotepad.MenuFeatures.AllNotes.MyNoteRecyclerView.MyNote;
import com.example.mynotepad.MenuFeatures.AllNotes.MyNoteRecyclerView.OnItemClickListener;
import com.example.mynotepad.MenuFeatures.AllNotes.Note.NoteFragment; import com.example.mynotepad.MenuFeatures.AllNotes.Note.NoteFragment;
import com.example.mynotepad.MenuFeatures.AllNotes.Note.NoteRecyclerView.MyAdaptor;
import com.example.mynotepad.MenuFeatures.AllNotes.Note.NoteRecyclerView.MyNote;
import com.example.mynotepad.MenuFeatures.AllNotes.NotesDataBase.Note; import com.example.mynotepad.MenuFeatures.AllNotes.NotesDataBase.Note;
import com.example.mynotepad.MenuFeatures.AllNotes.NotesDataBase.NoteDataBase; import com.example.mynotepad.MenuFeatures.AllNotes.NotesDataBase.NoteDataBase;
import com.example.mynotepad.R; import com.example.mynotepad.R;
...@@ -71,15 +63,17 @@ public class AllNotesActivity extends AppCompatActivity { ...@@ -71,15 +63,17 @@ public class AllNotesActivity extends AppCompatActivity {
allNoteListAndTitle = findViewById(R.id.allNoteListAndTitle); allNoteListAndTitle = findViewById(R.id.allNoteListAndTitle);
fragPlace = findViewById(R.id.fragmentPlace); fragPlace = findViewById(R.id.fragmentPlace);
fragPlace.setVisibility(View.GONE); fragPlace.setVisibility(View.GONE);
addListeners();
// show list with recycler view // show list with recycler view
recyclerView = findViewById(R.id.recyclerView); recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this , LinearLayoutManager.VERTICAL , false); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
myAdaptor = new MyAdaptor(myNotes, this); myAdaptor = new MyAdaptor(myNotes);
recyclerView.setAdapter(myAdaptor); recyclerView.setAdapter(myAdaptor);
recyclerView.setLayoutManager(linearLayoutManager); recyclerView.setLayoutManager(linearLayoutManager);
addListeners();
} }
...@@ -92,6 +86,28 @@ public class AllNotesActivity extends AppCompatActivity { ...@@ -92,6 +86,28 @@ public class AllNotesActivity extends AppCompatActivity {
}); });
//todo add listener for long click //update the data base //todo add listener for long click //update the data base
//todo add listener to recyclerView //todo add listener to recyclerView
myAdaptor.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClicked(MyNote myNote) {
//set noteFragment data
final Bundle bundle = new Bundle();
bundle.putString("title", myNote.getTitle());
bundle.putString("bodyTxt", myNote.getDescription());
final NoteFragment newNoteFrag = new NoteFragment();
newNoteFrag.setArguments(bundle);
//change the fragment
fragPlace.setVisibility(View.VISIBLE);
allNoteListAndTitle.setVisibility(View.GONE);
addNoteButton.hide();
replaceFragment(newNoteFrag);
}
@Override
public void onItemLongClick(MyNote item) {
}
});
} }
...@@ -124,11 +140,7 @@ public class AllNotesActivity extends AppCompatActivity { ...@@ -124,11 +140,7 @@ public class AllNotesActivity extends AppCompatActivity {
replaceFragment(newNoteFrag); replaceFragment(newNoteFrag);
} }
private void showChosenNote() { public void replaceFragment(Fragment fragment) {
//ToDo replcae fragment and update database
}
private void replaceFragment(Fragment fragment) {
fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(fragPlace.getId(), fragment); fragmentTransaction.replace(fragPlace.getId(), fragment);
fragmentTransaction.commit(); fragmentTransaction.commit();
...@@ -139,8 +151,9 @@ public class AllNotesActivity extends AppCompatActivity { ...@@ -139,8 +151,9 @@ public class AllNotesActivity extends AppCompatActivity {
notes = noteDataBase.noteDao().getAll(); notes = noteDataBase.noteDao().getAll();
myNotes = new ArrayList<>(); myNotes = new ArrayList<>();
for (Note note : notes) { for (Note note : notes) {
MyNote myNote = new MyNote(note.getTitle() , note.getDescription() , note.isAchieved()); MyNote myNote = new MyNote(note.getTitle(), note.getDescription(), note.isAchieved());
myNotes.add(myNote); myNotes.add(myNote);
} }
} }
} }
\ No newline at end of file
package com.example.mynotepad.MenuFeatures.AllNotes.MyNoteRecyclerView; package com.example.mynotepad.MenuFeatures.AllNotes.MyNoteRecyclerView;
public class MyAdaptor {
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 com.example.mynotepad.R;
import java.util.List;
public class MyAdaptor extends RecyclerView.Adapter<MyAdaptor.ViewHolder> {
private List<MyNote> notes;
private OnItemClickListener onItemClickListener;
public MyAdaptor(List<MyNote> notes) {
this.notes = notes;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_item_row, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.titleTV.setText(notes.get(position).getTitle());
holder.descriptionTV.setText(notes.get(position).getDescription());
holder.isAchieved = notes.get(position).isAchieved();
}
@Override
public int getItemCount() {
return notes.size();
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
protected TextView titleTV, descriptionTV;
protected boolean isAchieved;
public ViewHolder(View itemView) {
super(itemView);
titleTV = itemView.findViewById(R.id.titleTv);
descriptionTV = itemView.findViewById(R.id.descriptionTv);
isAchieved = false;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (onItemClickListener != null)
onItemClickListener.onItemClicked(notes.get(getAdapterPosition()));
}
@Override
public boolean onLongClick(View view) {
if (onItemClickListener != null)
onItemClickListener.onItemLongClick(notes.get(getAdapterPosition()));
return false;
}
}
} }
package com.example.mynotepad.MenuFeatures.AllNotes.MyNoteRecyclerView; package com.example.mynotepad.MenuFeatures.AllNotes.MyNoteRecyclerView;
public class MyNote { public class MyNote {
private String title , description ;
private boolean isAchieved;
public MyNote(String title, String description, boolean isAchieved) {
this.title = title;
this.description = description;
this.isAchieved = isAchieved;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public boolean isAchieved() {
return isAchieved;
}
} }
package com.example.mynotepad.MenuFeatures.AllNotes.MyNoteRecyclerView; package com.example.mynotepad.MenuFeatures.AllNotes.MyNoteRecyclerView;
public interface RecyclerViewOnClickListener { public interface OnItemClickListener {
void onItemClicked(MyNote item); void onItemClicked(MyNote item);
void onItemLongClick(MyNote item); void onItemLongClick(MyNote item);
} }
...@@ -4,6 +4,8 @@ import android.app.AlertDialog; ...@@ -4,6 +4,8 @@ import android.app.AlertDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import android.view.LayoutInflater; import android.view.LayoutInflater;
...@@ -35,9 +37,9 @@ public class NoteFragment extends Fragment { ...@@ -35,9 +37,9 @@ public class NoteFragment extends Fragment {
} }
@Override @Override
public void onPause() { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
//Todo show massage dialog if u did not save it super.onViewCreated(view, savedInstanceState);
super.onPause(); setTitleAndBodyTxt();
} }
private void init(View view) { private void init(View view) {
...@@ -54,9 +56,7 @@ public class NoteFragment extends Fragment { ...@@ -54,9 +56,7 @@ public class NoteFragment extends Fragment {
} }
private void showAlert() {
private void showAlert(){
final AlertDialog.Builder alert = new AlertDialog.Builder(getContext()); final AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
alert.setTitle("new note :"); alert.setTitle("new note :");
alert.setMessage("Do you want to save the note ?"); alert.setMessage("Do you want to save the note ?");
...@@ -66,12 +66,17 @@ public class NoteFragment extends Fragment { ...@@ -66,12 +66,17 @@ public class NoteFragment extends Fragment {
public void onClick(DialogInterface dialogInterface, int i) { public void onClick(DialogInterface dialogInterface, int i) {
saveNote(); saveNote();
Toast.makeText(getActivity(), "saved", Toast.LENGTH_SHORT).show(); Toast.makeText(getActivity(), "saved", Toast.LENGTH_SHORT).show();
getActivity().finish();
} }
}); });
alert.setNegativeButton("no", new DialogInterface.OnClickListener() { alert.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialogInterface, int i) { public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel(); dialogInterface.cancel();
if (getActivity() != null)
getActivity().finish();
} }
}); });
alert.show(); alert.show();
...@@ -79,7 +84,16 @@ public class NoteFragment extends Fragment { ...@@ -79,7 +84,16 @@ public class NoteFragment extends Fragment {
private void saveNote() { private void saveNote() {
note = new Note(titleTxt.getText().toString(), bodyTxt.getText().toString() , false); note = new Note(titleTxt.getText().toString(), bodyTxt.getText().toString(), false);
AllNotesActivity.noteDataBase.noteDao().insertNote(note); AllNotesActivity.noteDataBase.noteDao().insertNote(note);
} }
private void setTitleAndBodyTxt() {
Bundle bundle = getArguments();
if (bundle != null) {
titleTxt.setText(bundle.getString("title"));
bodyTxt.setText(bundle.getString("bodyTxt"));
}
}
} }
\ No newline at end of file
...@@ -6,6 +6,6 @@ ...@@ -6,6 +6,6 @@
<solid android:color="#76A0C3"/> <solid android:color="#76A0C3"/>
<stroke <stroke
android:width="13dp" android:width="13dp"
android:color="#9983B5DD"/> android:color="#4883B5DD"/>
<corners android:radius="0dp"/> <corners android:radius="0dp"/>
</shape> </shape>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"> android:shape="rectangle">
<solid android:color="#83B5DD"/> <solid android:color="#83B5DD"/>
<stroke <stroke
android:width="15dp" android:width="15dp"
android:color="#6A83B5DD"/> android:color="#4883B5DD"/>
<corners android:radius="0dp"/> <corners android:radius="0dp"/>
</shape> </shape>
\ No newline at end of file
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
android:background="@drawable/all_notes_bg" android:background="@drawable/all_notes_bg"
android:gravity="center" android:gravity="center"
android:text="@string/all_notes" android:text="@string/all_notes"
android:textColor="#020930"
android:textSize="20dp" /> android:textSize="20dp" />
<com.example.mynotepad.MenuFeatures.AllNotes.CustomToolbarOption <com.example.mynotepad.MenuFeatures.AllNotes.CustomToolbarOption
...@@ -45,7 +46,7 @@ ...@@ -45,7 +46,7 @@
android:id="@+id/recyclerView" android:id="@+id/recyclerView"
android:layout_width="409dp" android:layout_width="409dp"
android:layout_height="601dp" android:layout_height="601dp"
android:background="#9983B5DD" android:background="#4883B5DD"
tools:layout_editor_absoluteX="1dp" tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp" /> tools:layout_editor_absoluteY="1dp" />
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="57dp" android:layout_height="60dp"
android:orientation="horizontal" android:orientation="horizontal"
android:background="@drawable/row_bg"> android:background="@drawable/row_bg">
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
android:layout_marginLeft="12dp" android:layout_marginLeft="12dp"
android:lines="1" android:lines="1"
android:text="body text" android:text="body text"
android:textSize="15dp" /> android:textSize="12dp" />
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment