Skip to content

Instantly share code, notes, and snippets.

@thundercipher
Created May 22, 2020 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thundercipher/fc0b47f6dc458091333c8dcf289f9efe to your computer and use it in GitHub Desktop.
Save thundercipher/fc0b47f6dc458091333c8dcf289f9efe to your computer and use it in GitHub Desktop.
Code of the MainActivity file
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.HashSet;
public class MainActivity extends AppCompatActivity {
static ArrayList<String> notes = new ArrayList<String>();
static ArrayAdapter<String> arrayAdapter;
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item)
{
super.onOptionsItemSelected(item);
if(item.getItemId() == R.id.add_note)
{
Intent intent = new Intent(getApplicationContext(), NoteEditorActivity.class);
startActivity(intent);
return true;
}
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView)findViewById(R.id.listView);
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.tanay.thunderbird.deathnote", Context.MODE_PRIVATE);
HashSet<String> set = (HashSet<String>)sharedPreferences.getStringSet("notes", null);
if(set == null)
{
notes.add("Example Note");
}
else
{
notes = new ArrayList<>(set); // to bring all the already stored data in the set to the notes ArrayList
}
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, notes);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent intent = new Intent(getApplicationContext(), NoteEditorActivity.class);
intent.putExtra("noteID", position);
startActivity(intent);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id)
{
new AlertDialog.Builder(MainActivity.this) // we can't use getApplicationContext() here as we want the activity to be the context, not the application
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Delete?")
.setMessage("Are you sure you want to delete this note?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) // to remove the selected note once "Yes" is pressed
{
notes.remove(position);
arrayAdapter.notifyDataSetChanged();
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.tanay.thunderbird.deathnote", Context.MODE_PRIVATE);
HashSet<String> set = new HashSet<>(MainActivity.notes);
sharedPreferences.edit().putStringSet("notes", set).apply();
}
})
.setNegativeButton("No", null)
.show();
return true; // this was initially false but we change it to true as if false, the method assumes that we want to do a short click after the long click as well
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment