Search code examples
androidlistviewandroid-arrayadapternotifydatasetchanged

ArrayAdapter notifyDataSetChanged from AlertDialog doesn't work


I show items from my SQLite database in a Listview. For adding Items I use a AlertDialog. In the ALertDialog I save the item in my database and refresh the List (userLists) with the data from the database. Then I use notifyDataSetChanged but the view does't change.

public class MediUserListsFragment extends Fragment {

    private MediDAO mediDAO;
    private ListView listView;
    private List<UserList> userLists;
    private ArrayAdapter<UserList> adapter;

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        getActivity().setTitle("Medikamenten Listen");

        final View view =  inflater.inflate(R.layout.fragment_medi_user_list, container, false);

        setHasOptionsMenu(true);

        mediDAO = new MediDAO(getContext());

        userLists = mediDAO.getAllUserList();

        listView = (ListView) view.findViewById(R.id.list_view);

        adapter = new ArrayAdapter<>(getContext(),android.R.layout.simple_list_item_1, userLists);
        listView.setAdapter(adapter);

        Button newLstBtn = (Button) view.findViewById(R.id.new_list_button);
        newLstBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View alertLayout = inflater.inflate(R.layout.dialog_medi_list_new, null);
                final EditText newLstName = (EditText) alertLayout.findViewById(R.id.newListName);
                AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
                alert.setTitle("Neue Liste");
                alert.setView(alertLayout);
                alert.setCancelable(false);
                alert.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                alert.setPositiveButton("Erstellen", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mediDAO.newUserList(newLstName.getText().toString());
                        userLists = mediDAO.getAllUserList();
                        adapter.notifyDataSetChanged();
                    }
                });
                AlertDialog dialog = alert.create();
                dialog.show();
            }
        });

        return view;
    }

    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.clear();
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.fragment_medi_abc, menu);
    }
}

Solution

  • In your setPositiveButton, try the following.

    userLists.clear();
    userLists.addAll(mediDAO.getAllUserList());                      
    adapter.notifyDataSetChanged();
    

    When you were doing userLists = mediDAO.getAllUserList(); the adapter looses the original data object's reference (which is userLists). With userLists.addAll(), the adapter knows the data object it created with has changed hence, notifyDataSetChanged finds changes to notify.