Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- private ListView mListViewNotes;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mListViewNotes = findViewById(R.id.main_listview_notes);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.action_main_new_note:
- Intent newActivity = new Intent(this, QuestionsActivity.class);
- startActivity(newActivity);
- break;
- }
- return true;
- }
- @Override
- protected void onResume() {
- super.onResume();
- mListViewNotes.setAdapter(null);
- ArrayList<Note> notes = Utilities.getAllSavedNotes(this);
- if (notes ==null || notes.size() == 0) {
- Toast.makeText(this, "no saved notes", Toast.LENGTH_SHORT).show();
- return;
- }else {
- NoteAdapter na = new NoteAdapter(this, R.layout.item_note, notes);
- mListViewNotes.setAdapter(na);
- mListViewNotes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
- String fileName = ((Note)mListViewNotes.getItemAtPosition(position)).getDateTime()
- + Utilities.FILE_EXTENSION;
- Intent viewNoteIntent = new Intent(getApplicationContext(),QuestionsActivity.class);
- viewNoteIntent.putExtra("NOTE_FILE", fileName);
- startActivity(viewNoteIntent);
- }
- });
- }
- }
- public class QuestionsActivity extends AppCompatActivity {
- ListView lv;
- String[] charactersDC = {"question 1", "question 2", "question 3", "question 4", "question 5"};
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_questions);
- lv = (ListView) findViewById(R.id.idListView);
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, charactersDC);
- lv.setAdapter(adapter);
- lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- if(position == 0)
- {
- Intent myIntent = new Intent(QuestionsActivity.this, Question1.class);
- myIntent.putExtra("NOTE_FILE", getIntent().getStringExtra("NOTE_FILE"));
- startActivityForResult(myIntent, 0);
- }
- if(position == 1)
- {
- Intent myIntent = new Intent(QuestionsActivity.this, Question2.class);
- startActivityForResult(myIntent, 0);
- }
- }
- });
- }
- public class Question1 extends AppCompatActivity {
- private EditText mEtTitle;
- private String mNoteFileName;
- private Ideas mLoadedNote;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_question1);
- mEtTitle = (EditText) findViewById(R.id.note_et_title);
- mNoteFileName = getIntent().getStringExtra("NOTE_FILE");
- if(mNoteFileName !=null && !mNoteFileName.isEmpty()) {
- mLoadedNote = Utilities.getNoteByName(this, mNoteFileName);
- if(mLoadedNote !=null) {
- mEtTitle.setText(mLoadedNote.getTitle());
- }
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.menu_note_new, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.action_note_save:
- saveNote();
- break;
- }
- return true;
- }
- private void saveNote() {
- Note note;
- if(mLoadedNote ==null) {
- note = new Note(System.currentTimeMillis(), mEtTitle.getText().toString());
- }else {
- note = new Note(mLoadedNote.getDateTime(), mEtTitle.getText().toString());
- }
- if (Utilities.saveNote(this, note)){
- Toast.makeText(this, "saved", Toast.LENGTH_SHORT).show();
- }else{
- Toast.makeText(this, "not enough space", Toast.LENGTH_SHORT).show();
- }
- finish();
- }
- public static final String FILE_EXTENSION = ".bin";
- public static boolean saveNote(Context context, Note note) {
- String fileName = String.valueOf(note.getDateTime()) + FILE_EXTENSION;
- FileOutputStream fos;
- ObjectOutputStream oos;
- try {
- fos = context.openFileOutput(fileName, context.MODE_PRIVATE);
- oos = new ObjectOutputStream(fos);
- oos.writeObject(note);
- oos.close();
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- public static ArrayList<Note> getAllSavedNotes(Context context){
- ArrayList<Note> notes = new ArrayList<>();
- File filesDir = context.getFilesDir();
- ArrayList<String> noteFiles = new ArrayList<>();
- for(String file : filesDir.list()) {
- if(file.endsWith(FILE_EXTENSION)) {
- noteFiles.add(file);
- }
- }
- FileInputStream fis;
- ObjectInputStream ois;
- for(int i = 0; i < noteFiles.size(); i++) {
- try{
- fis = context.openFileInput(noteFiles.get(i));
- ois = new ObjectInputStream(fis);
- notes.add((Note) ois.readObject());
- fis.close();
- ois.close();
- } catch (IOException | ClassNotFoundException e) {
- e.printStackTrace();
- return null;
- }
- }
- return notes;
- }
- public static Note getNoteByName(Context context, String fileName) {
- File file = new File(context.getFilesDir(), fileName);
- Note note;
- if(file.exists()) {
- FileInputStream fis;
- ObjectInputStream ois;
- try{
- fis = context.openFileInput(fileName);
- ois = new ObjectInputStream(fis);
- note = (Note) ois.readObject();
- fis.close();
- ois.close();
- } catch (IOException | ClassNotFoundException e) {
- e.printStackTrace();
- return null;
- }
- return note;
- }
- return null;
- }
Add Comment
Please, Sign In to add comment