Guest User

Untitled

a guest
Aug 17th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.33 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2. private ListView mListViewNotes;
  3.  
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8.  
  9. mListViewNotes = findViewById(R.id.main_listview_notes);
  10. }
  11.  
  12. @Override
  13. public boolean onCreateOptionsMenu(Menu menu) {
  14. getMenuInflater().inflate(R.menu.menu_main, menu);
  15. return true;
  16. }
  17.  
  18. @Override
  19. public boolean onOptionsItemSelected(MenuItem item) {
  20. switch (item.getItemId()) {
  21. case R.id.action_main_new_note:
  22. Intent newActivity = new Intent(this, QuestionsActivity.class);
  23. startActivity(newActivity);
  24. break;
  25. }
  26.  
  27. return true;
  28. }
  29.  
  30. @Override
  31. protected void onResume() {
  32. super.onResume();
  33. mListViewNotes.setAdapter(null);
  34.  
  35. ArrayList<Note> notes = Utilities.getAllSavedNotes(this);
  36.  
  37. if (notes ==null || notes.size() == 0) {
  38. Toast.makeText(this, "no saved notes", Toast.LENGTH_SHORT).show();
  39. return;
  40. }else {
  41. NoteAdapter na = new NoteAdapter(this, R.layout.item_note, notes);
  42. mListViewNotes.setAdapter(na);
  43.  
  44. mListViewNotes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  45. @Override
  46. public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
  47. String fileName = ((Note)mListViewNotes.getItemAtPosition(position)).getDateTime()
  48. + Utilities.FILE_EXTENSION;
  49.  
  50. Intent viewNoteIntent = new Intent(getApplicationContext(),QuestionsActivity.class);
  51. viewNoteIntent.putExtra("NOTE_FILE", fileName);
  52. startActivity(viewNoteIntent);
  53. }
  54. });
  55. }
  56. }
  57.  
  58. public class QuestionsActivity extends AppCompatActivity {
  59. ListView lv;
  60. String[] charactersDC = {"question 1", "question 2", "question 3", "question 4", "question 5"};
  61.  
  62.  
  63.  
  64. @Override
  65. protected void onCreate(Bundle savedInstanceState) {
  66. super.onCreate(savedInstanceState);
  67. setContentView(R.layout.activity_questions);
  68.  
  69. lv = (ListView) findViewById(R.id.idListView);
  70. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, charactersDC);
  71. lv.setAdapter(adapter);
  72. lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  73. @Override
  74. public void onItemClick(AdapterView<?> parent, View view,
  75. int position, long id) {
  76.  
  77. if(position == 0)
  78. {
  79.  
  80. Intent myIntent = new Intent(QuestionsActivity.this, Question1.class);
  81. myIntent.putExtra("NOTE_FILE", getIntent().getStringExtra("NOTE_FILE"));
  82. startActivityForResult(myIntent, 0);
  83. }
  84.  
  85. if(position == 1)
  86. {
  87.  
  88. Intent myIntent = new Intent(QuestionsActivity.this, Question2.class);
  89. startActivityForResult(myIntent, 0);
  90. }
  91. }
  92. });
  93.  
  94. }
  95.  
  96. public class Question1 extends AppCompatActivity {
  97.  
  98. private EditText mEtTitle;
  99. private String mNoteFileName;
  100. private Ideas mLoadedNote;
  101.  
  102. @Override
  103. protected void onCreate(Bundle savedInstanceState) {
  104. super.onCreate(savedInstanceState);
  105. setContentView(R.layout.activity_question1);
  106.  
  107. mEtTitle = (EditText) findViewById(R.id.note_et_title);
  108.  
  109. mNoteFileName = getIntent().getStringExtra("NOTE_FILE");
  110. if(mNoteFileName !=null && !mNoteFileName.isEmpty()) {
  111. mLoadedNote = Utilities.getNoteByName(this, mNoteFileName);
  112.  
  113. if(mLoadedNote !=null) {
  114. mEtTitle.setText(mLoadedNote.getTitle());
  115. }
  116.  
  117. }
  118. }
  119.  
  120. @Override
  121. public boolean onCreateOptionsMenu(Menu menu) {
  122. getMenuInflater().inflate(R.menu.menu_note_new, menu);
  123. return true;
  124. }
  125.  
  126. @Override
  127. public boolean onOptionsItemSelected(MenuItem item) {
  128.  
  129. switch (item.getItemId()) {
  130.  
  131. case R.id.action_note_save:
  132. saveNote();
  133.  
  134. break;
  135. }
  136.  
  137. return true;
  138. }
  139.  
  140. private void saveNote() {
  141. Note note;
  142.  
  143. if(mLoadedNote ==null) {
  144. note = new Note(System.currentTimeMillis(), mEtTitle.getText().toString());
  145.  
  146. }else {
  147. note = new Note(mLoadedNote.getDateTime(), mEtTitle.getText().toString());
  148.  
  149. }
  150.  
  151. if (Utilities.saveNote(this, note)){
  152. Toast.makeText(this, "saved", Toast.LENGTH_SHORT).show();
  153.  
  154. }else{
  155. Toast.makeText(this, "not enough space", Toast.LENGTH_SHORT).show();
  156.  
  157. }
  158.  
  159. finish();
  160. }
  161.  
  162. public static final String FILE_EXTENSION = ".bin";
  163.  
  164.  
  165. public static boolean saveNote(Context context, Note note) {
  166.  
  167. String fileName = String.valueOf(note.getDateTime()) + FILE_EXTENSION;
  168.  
  169. FileOutputStream fos;
  170. ObjectOutputStream oos;
  171.  
  172. try {
  173. fos = context.openFileOutput(fileName, context.MODE_PRIVATE);
  174. oos = new ObjectOutputStream(fos);
  175. oos.writeObject(note);
  176. oos.close();
  177. fos.close();
  178.  
  179. } catch (IOException e) {
  180. e.printStackTrace();
  181. return false;
  182.  
  183.  
  184. }
  185.  
  186. return true;
  187. }
  188.  
  189. public static ArrayList<Note> getAllSavedNotes(Context context){
  190. ArrayList<Note> notes = new ArrayList<>();
  191.  
  192. File filesDir = context.getFilesDir();
  193. ArrayList<String> noteFiles = new ArrayList<>();
  194.  
  195. for(String file : filesDir.list()) {
  196. if(file.endsWith(FILE_EXTENSION)) {
  197. noteFiles.add(file);
  198. }
  199. }
  200.  
  201. FileInputStream fis;
  202. ObjectInputStream ois;
  203.  
  204. for(int i = 0; i < noteFiles.size(); i++) {
  205. try{
  206. fis = context.openFileInput(noteFiles.get(i));
  207. ois = new ObjectInputStream(fis);
  208.  
  209. notes.add((Note) ois.readObject());
  210.  
  211. fis.close();
  212. ois.close();
  213.  
  214. } catch (IOException | ClassNotFoundException e) {
  215. e.printStackTrace();
  216. return null;
  217. }
  218. }
  219.  
  220. return notes;
  221. }
  222.  
  223.  
  224. public static Note getNoteByName(Context context, String fileName) {
  225. File file = new File(context.getFilesDir(), fileName);
  226. Note note;
  227.  
  228. if(file.exists()) {
  229. FileInputStream fis;
  230. ObjectInputStream ois;
  231.  
  232. try{
  233. fis = context.openFileInput(fileName);
  234. ois = new ObjectInputStream(fis);
  235.  
  236. note = (Note) ois.readObject();
  237.  
  238. fis.close();
  239. ois.close();
  240.  
  241. } catch (IOException | ClassNotFoundException e) {
  242. e.printStackTrace();
  243. return null;
  244. }
  245.  
  246. return note;
  247. }
  248.  
  249. return null;
  250. }
Add Comment
Please, Sign In to add comment