Skip to content
Advertisement

notifyDataSetChanged(); not refreshing ListView?

I’m working on a notes app and I’m running into trouble deleting notes: after the note is deleted, it still shows in the listview.

I’m trying to use notifyDataSetChanged(), but it doesn’t seem to be working for me.

MainActivity:

public class MainActivity extends AppCompatActivity {

    private ListView mListViewNotes;
    ArrayAdapter<Note> listAdapter;
    ArrayList<Note> list_items = new ArrayList<>();
    int count = 0;
    String list_item;
    Object mActionMode;
    Toolbar app_bar;
    ArrayList<Note> notes;
    private String mNoteFileName;
    private Note mLoadedNote;
    Note loadedNote;
    ImageView picTest;
    NoteAdapter na;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        app_bar = (Toolbar) findViewById(R.id.app_bar);
        TextView title_1 = (TextView) findViewById(R.id.title1);
        TextView title_2 = (TextView) findViewById(R.id.title2);
        TextView title_m = (TextView) findViewById(R.id.title_m);
        Typeface music_font = Typeface.createFromAsset(getAssets(), "fonts/melodymakernotesonly.ttf");
        Typeface notes_font = Typeface.createFromAsset(getAssets(), "fonts/the unseen.ttf");
        setSupportActionBar(app_bar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setElevation(12);
        title_1.setTypeface(music_font);
        title_2.setTypeface(notes_font);
        title_m.setTypeface(music_font);

        listAdapter = new ArrayAdapter<Note>(this, R.layout.item_note, R.id.item_note, list_items);

        mNoteFileName = getIntent().getStringExtra("NOTE_FILE");
        if (mNoteFileName != null && !mNoteFileName.isEmpty()) {
            loadedNote = Utilities.getNoteByName(this, mNoteFileName);
        }

        mListViewNotes = (ListView) findViewById(R.id.listview_notes);
        mListViewNotes.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        mListViewNotes.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
                if (list_items.contains(notes.get(position))) {
                    count = count-1;
                    list_items.remove(notes.get(position));
                    mode.setTitle(count + " Notes Selected");
                } else {
                    count = count+1;
                    list_items.add(notes.get(position));
                    mode.setTitle(count + " Notes Selected");
                }

                if (count == 0) {
                    mode.setTitle("No Notes Selected");
                }
            }

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {

                app_bar.setVisibility(View.GONE);
                MenuInflater inflater = mode.getMenuInflater();
                inflater.inflate(R.menu.context_menu, menu);
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                deleteNote();
                return true;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                count = 0;
                app_bar.setVisibility(View.VISIBLE);
                list_items.clear();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add_note:
                startActivity(new Intent(this, NoteActivity.class));
                break;
            case R.id.action_settings:
                Log.d("list:", "testing");
        }

        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mListViewNotes.setAdapter(null);

        notes = Utilities.getAllSavedNotes(this);

        if (notes == null || notes.size() == 0) {
            Toast.makeText(this, "No Notes!", Toast.LENGTH_LONG).show();
        } else {
            na = new NoteAdapter(this, R.layout.item_note, notes);
            mListViewNotes.setAdapter(na);

            mListViewNotes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String filename = ((Note) mListViewNotes.getItemAtPosition(position)).getDateTime() + Utilities.FileExtention;

                    Intent view_note = new Intent(getApplicationContext(), NoteActivity.class);
                    view_note.putExtra("NOTE_FILE", filename);
                    startActivity(view_note);
                }
            });
        }
    }

    private void deleteNote() {

            if(list_items.contains(null)) {
                finish();
            } else {
                AlertDialog.Builder dialog = new AlertDialog.Builder(this)
                        .setTitle("Delete")
                        .setMessage("Are you sure?")
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                for (Note loadedNote : list_items) {
                                    Utilities.deleteNote(getApplicationContext(), loadedNote.getDateTime() + Utilities.FileExtention);
                                }
                                na.notifyDataSetChanged();
                                listAdapter.notifyDataSetChanged();
                            }
                        })
                        .setNegativeButton("No", null)
                        .setCancelable(false);
                dialog.show();
            }
    }

NoteAdapter

    public class NoteAdapter extends ArrayAdapter<Note> {

    public NoteAdapter(Context context, int resource, ArrayList<Note> notes) {
        super(context, resource, notes);



    }

    @Override
    public View getView(int position, View convertView, @NonNull ViewGroup parent) {
        //return super.getView(position, convertView, parent);

        if(convertView == null) {
            convertView = LayoutInflater.from(getContext())
                    .inflate(R.layout.item_note, null);
        }

        Note note = getItem(position);

        if(note != null) {
            TextView title = (TextView) convertView.findViewById(R.id.list_note_title);
            TextView content = (TextView) convertView.findViewById(R.id.list_note_content);
            TextView date = (TextView) convertView.findViewById(R.id.list_note_date);

            Typeface scribble_card = Typeface.createFromAsset(getContext().getAssets(), "fonts/the unseen.ttf");
            title.setTypeface(scribble_card);
            content.setTypeface(scribble_card);

            title.setText(note.getTitle());
            date.setText(note.getDateTimeFormatted(getContext()));

            if(note.getContent().length() > 25) {
                content.setText(note.getContent().substring(0,25) + "...");
            } else {
                content.setText(note.getContent());
            }

            if(note.getContent().length() <= 0) {
                content.setText("(Empty Note..)");
            } else {
                content.setText(note.getContent());
            }

            if (note.getTitle().length() <= 0) {
                title.setText("(Untitled)");
            } else {
                title.setText(note.getTitle());
            }
        }

        return convertView;
    }

Advertisement

Answer

You are setting adapter with a list:

 na = new NoteAdapter(this, R.layout.item_note, notes);
    mListViewNotes.setAdapter(na);

na.notifyDataSetChanged(); will only work if your notes list is changed.

And I don’t see that you remove/add/modify on notes list. If you are deleting item from list you need to remove it from notes list, too:

notes.remove(position_to_remove);
na.notifyDataSetChanged();
7 People found this is helpful
Advertisement