0

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);
    }
benruty
  • 89
  • 1
  • 1
  • 7
  • It would probably help if you edited the question to include the Stack Trace from the log. – MikeT Aug 04 '17 at 22:37
  • i have done that sir. you can do well to review – benruty Aug 04 '17 at 22:44
  • Which line is line 123 (as per `at com.app.ben.notetaker.Utilities.deleteFile(Utilities.java:123)`) in Utilities, at a guess the passed Context is null so `File dirFiles = context.getFilesDir();` is line 123. – MikeT Aug 04 '17 at 22:58
  • You might find this of assistance [Get application context returns null](https://stackoverflow.com/questions/21994612/get-application-context-returns-null) i.e. your issue may be this line in MainActivity `Utilities.deleteFile(getApplicationContext(), mFileName)`, in that `getApplicationContext` may be resolving to null; – MikeT Aug 04 '17 at 23:08
  • i have resolved the issue. No longer having null pointer error. However, i cant delete the list view item. It shows the toast but the list view item doesnt disappear. You can review my updated code – benruty Aug 04 '17 at 23:19
  • You need to amend the ListView's source (adapter or cursor) and then use `adapter.notifyDataSetChanged();` (adpater.swapCursor(cursor) is also an option for cursors). Note if an ArrayAdapter this link may be helpful [notifyDataSetChanged example](https://stackoverflow.com/questions/3669325/notifydatasetchanged-example) – MikeT Aug 04 '17 at 23:31

0 Answers0