-1

I have four fragments in a tab layout. In one of the fragments, I am displaying 10 notes in a listview. In addition to that, I am giving the user the option to add new notes using a dialog box. So, on adding a new note, the listview gets refreshed to show the new note as well but when I switch to another tab and then get back to my original tab, the new note is not displayed in the listview. How to solve this problem?

This is my java code:

public class NoteFragment extends Fragment {

ListView lv_notes;
Button btn_newNote;
ArrayList<NotesModel> notesModelArrayList;
private static NotesAdapter notesAdapter;
NotesModel newNote;

public NoteFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_note, container, false);

    lv_notes = (ListView)rootView.findViewById(R.id.lv_notes);

    btn_newNote = (Button)rootView.findViewById(R.id.btn_newNote);

    notesModelArrayList = new ArrayList<>();

    for (int i = 1; i <= 10; i++){
        notesModelArrayList.add(new NotesModel("Note " + i,"24/05/2017"));
    }

    notesAdapter = new NotesAdapter(notesModelArrayList, getContext());

    lv_notes.setAdapter(notesAdapter);

    btn_newNote.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LayoutInflater newNoteInflater = LayoutInflater.from(getContext());
            View newNoteView = newNoteInflater.inflate(R.layout.noteprompt,null);

            final AlertDialog.Builder noteDialogBuilder = new AlertDialog.Builder(getContext());

            noteDialogBuilder.setView(newNoteView);

            final EditText et_newNote = (EditText)newNoteView.findViewById(R.id.et_newNote);

            noteDialogBuilder.setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    newNote = new NotesModel(et_newNote.getText().toString(),LogFragment.day_d + "/" + (LogFragment.month_d + 1) + "/" + LogFragment.year_d);

                    notesModelArrayList.add(newNote);
                    notesAdapter = new NotesAdapter(notesModelArrayList, getContext());

                    lv_notes.setAdapter(notesAdapter);
                }
            }).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            AlertDialog newNoteDialog = noteDialogBuilder.create();
            newNoteDialog.show();
        }
    });

    return rootView;
}
}
Mayank Aggarwal
  • 139
  • 1
  • 4
  • 14
  • 1
    notesAdapter.notifyDataSetChanged(); use this after setting your adapter,also you need to clear your arraylist – Sunisha Guptan May 24 '17 at 08:29
  • *notesAdapter.notifyDataSetChanged(); use this after seting your adapter* @SunishaSindhu what for? it sounds like [Million Monkeys Programming Style](https://en.wikipedia.org/wiki/Programming_by_permutation) **the problem is that he never save the data** – Selvin May 24 '17 at 08:31
  • @Selvin : i didn't get you.what you mean? – Sunisha Guptan May 24 '17 at 08:36
  • also read this https://stackoverflow.com/questions/3669325/notifydatasetchanged-example you will understand why i said that – Sunisha Guptan May 24 '17 at 08:37
  • 1
    @selvin : I think `newNote = new NotesModel(et_newNote.getText().toString(),LogFragment.day_d + "/" + (LogFragment.month_d + 1) + "/" + LogFragment.year_d); notesModelArrayList.add(newNote);` saves the data – Debu May 24 '17 at 08:38
  • @SunishaSindhu lol ... the problem is that he never save the data. **Calling `notifyDataSetChanged` right after `setAdapter` is nonsens** – Selvin May 24 '17 at 08:38
  • @Selvin How do I save the data? – Mayank Aggarwal May 24 '17 at 08:40
  • declare `notesModelArrayList` in the activity then the data will not lose, but also be careful about adding and deleting new data. – Debu May 24 '17 at 08:43
  • @Debu no, it doesn't save data ... it creates new object and add it to array list ... obviously in next onCreateView call it will be wiped – Selvin May 24 '17 at 08:44
  • What do u mean by saving the data???? @Selvin – Debu May 24 '17 at 08:45
  • check if your fragment's `onCreateView()` is called during tab swipe ? – rafsanahmad007 May 24 '17 at 08:45
  • saving data is obviously putting it into some persistent storage – Selvin May 24 '17 at 08:46
  • if the notesModelArrayList is in the activity that is hosting the tablayout then how the data gets removed on oncreateview(I am talking of per session, not after the app is closed)?????? – Debu May 24 '17 at 08:48
  • as we have `notesModelArrayList = new ArrayList<>()` inside `onCreateView` then yes, of course *the data gets removed on oncreateview* – Selvin May 24 '17 at 08:52

2 Answers2

1

Your problem is caused by the fragment lifecycle. In case that it is destroyed, the new instance of the fragment will not keep the updated list, but the original one. To fix this, you have to save the array:

Save the data when fragment is about to be destroyed:

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelableArrayList("notesList", notesModelArrayList);
}

And retrieve/create the data when fragment is created:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        notesModelArrayList = savedInstanceState.getParcelableArrayList("notesList");
    } else {
        for (int i = 1; i <= 10; i++){
            notesModelArrayList.add(new NotesModel("Note " + i,"24/05/2017"));
        }
    }
}

Beside this, I have 2 things to mention: you should implement Parcelable in the NotesModel class to be able to save them and, this will only work if you don't close the app. If you want a persistent solution, please consider using SharedPreferences.

EDIT - forgot to mention that you should remove the following lines from onCreateView:

notesModelArrayList = new ArrayList<>();

for (int i = 1; i <= 10; i++){
    notesModelArrayList.add(new NotesModel("Note " + i,"24/05/2017"));
}
Iulian Popescu
  • 2,595
  • 4
  • 23
  • 31
-1

Instead of these 2 lines

notesAdapter = new NotesAdapter(notesModelArrayList, getContext());

lv_notes.setAdapter(notesAdapter);

try calling notesAdapter.notifyDatasetChanged(); inside setPositiveButton onclick.

Debu
  • 615
  • 7
  • 21