|
| 1 | +package com.lenovo.notetakingapp; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.DialogInterface; |
| 5 | +import android.content.Intent; |
| 6 | +import android.content.SharedPreferences; |
| 7 | +import android.support.v7.app.AlertDialog; |
| 8 | +import android.support.v7.app.AppCompatActivity; |
| 9 | +import android.os.Bundle; |
| 10 | +import android.util.Log; |
| 11 | +import android.view.Menu; |
| 12 | +import android.view.MenuInflater; |
| 13 | +import android.view.MenuItem; |
| 14 | +import android.view.View; |
| 15 | +import android.widget.AdapterView; |
| 16 | +import android.widget.ArrayAdapter; |
| 17 | +import android.widget.ListView; |
| 18 | +import android.widget.Toast; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.HashSet; |
| 22 | + |
| 23 | +public class MainActivity extends AppCompatActivity { |
| 24 | + |
| 25 | + static ArrayList<String> notes = new ArrayList<>(); |
| 26 | + static ArrayAdapter arrayAdapter; |
| 27 | + |
| 28 | + @Override |
| 29 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 30 | + MenuInflater menuInflater = getMenuInflater(); |
| 31 | + menuInflater.inflate(R.menu.add_note_menu,menu); |
| 32 | + return super.onCreateOptionsMenu(menu); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 37 | + super.onOptionsItemSelected(item); |
| 38 | + if(item.getItemId()==R.id.add_note) |
| 39 | + { |
| 40 | + Intent intent = new Intent(getApplicationContext(),NoteEditorActivity.class); |
| 41 | + startActivity(intent); |
| 42 | + return true; |
| 43 | + } |
| 44 | + return false; |
| 45 | + |
| 46 | + } |
| 47 | + @Override |
| 48 | + protected void onCreate(Bundle savedInstanceState) { |
| 49 | + super.onCreate(savedInstanceState); |
| 50 | + setContentView(R.layout.activity_main); |
| 51 | + ListView listView = (ListView)findViewById(R.id.listView); |
| 52 | + final SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.lenovo.notetakingapp", Context.MODE_PRIVATE); |
| 53 | + final HashSet<String> set = (HashSet<String>) sharedPreferences.getStringSet("notes",null); |
| 54 | + if (set ==null) |
| 55 | + { |
| 56 | + notes.add("Example notes"); |
| 57 | + |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + notes = new ArrayList(set); |
| 62 | + } |
| 63 | + arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,notes); |
| 64 | + listView.setAdapter(arrayAdapter); |
| 65 | + |
| 66 | + listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 67 | + @Override |
| 68 | + public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 69 | + Intent intent = new Intent(getApplicationContext(),NoteEditorActivity.class); |
| 70 | + intent.putExtra("noteId",position); |
| 71 | + startActivity(intent); |
| 72 | + } |
| 73 | + }); |
| 74 | + listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){ |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) { |
| 78 | + new AlertDialog.Builder(MainActivity.this) |
| 79 | + .setIcon(android.R.drawable.ic_dialog_alert) |
| 80 | + .setTitle("Are you Sure") |
| 81 | + .setMessage("Do you want to delete this note!!") |
| 82 | + .setPositiveButton("Yes",new DialogInterface.OnClickListener(){ |
| 83 | + |
| 84 | + @Override |
| 85 | + public void onClick(DialogInterface dialog, int which) { |
| 86 | + notes.remove(position); |
| 87 | + arrayAdapter.notifyDataSetChanged(); |
| 88 | + HashSet<String> set = new HashSet<String>(MainActivity.notes); |
| 89 | + sharedPreferences.edit().putStringSet("notes",set).apply(); |
| 90 | + |
| 91 | + } |
| 92 | + }) |
| 93 | + .setNegativeButton("No",null) |
| 94 | + .show(); |
| 95 | + |
| 96 | + |
| 97 | + return true; |
| 98 | + } |
| 99 | + } |
| 100 | + ); |
| 101 | + } |
| 102 | +} |
0 commit comments