I am working on a notes app. I implemented a context menu on each list view item which pops up a delete option. When the delete option is selected,an alert dialog pops up asking if user wants to delete the selected note. I also have a Utilities class that controls the delete operation. This issue is that i am unable to implement the right logic for the note as the selected note item doesn't disappear.
Utilities.java
public class Utilities {
/**
* String extra for a note's filename
*/
public static final String EXTRAS_NOTE_FILENAME = "EXTRAS_NOTE_FILENAME";
public static final String FILE_EXTENSION = ".bin";
public static boolean deleteFile(Context context, String fileName) {
File dirFiles = context.getFilesDir();
File file = new File(dirFiles, fileName);
if(file.exists() && !file.isDirectory()) {
return file.delete();
}
return false;
}
}
Main Activity
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
final Note mLoadedNote = (Note) mListNotes.getAdapter().getItem(info.position);
mLoadedNote.getTitle();
switch (item.getItemId()) {
break;
case R.id.delete:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this)
.setTitle("Delete " + mLoadedNote.getTitle())
.setMessage("are you sure?")
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mFileName = mListNotes.getAdapter().getItem(info.position) + (Utilities.EXTRAS_NOTE_FILENAME);
if(Utilities.deleteFile(getApplicationContext(), mFileName) ) {
Toast.makeText(MainActivity.this, mLoadedNote.getTitle() + " is deleted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "can not delete the note '" + mLoadedNote.getTitle() + "'", Toast.LENGTH_SHORT).show();
}
}
})
.setNegativeButton("NO", null); //do nothing on clicking NO button :P
alertDialog.show();
}
return super.onContextItemSelected(item);
}