Search code examples
androidsimplecursoradapternotifydatasetchanged

How do I properly use notifyDataSetChanged in my code?


I am populating a ListView with a custom SimpleCursorAdapter.

I have a delete function that deletes a person from the database that is backing the ListView.

I cannot figure out where to put notifyDataSetChanged() so that my list refreshes to show the person has been removed.

I am using a SwipeRefreshLayout that will show that the person has been removed if I swipe, but I would prefer if the list would update automatically when the person was removed.

Where would I use notifyDataSetChanged() in my code?

Home.java

public class Home extends Fragment implements TabChanger{

    private View rootView;
    private ViewPager myViewPager;
    private SwipeRefreshLayout personSwipeRefresh;

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

        rootView = inflater.inflate(R.layout.home, container, false);
        myViewPager = (ViewPager) getActivity().findViewById(R.id.pager);
        personSwipeRefresh = (SwipeRefreshLayout) rootView.findViewById(R.id.person_swipe_refresh);

        return rootView;
    }

    @Override
    public void onViewCreated(View rootView, Bundle savedInstanceState) {
        super.onViewCreated(rootView, savedInstanceState);

        drawThePersonView();

        personSwipeRefresh.setColorSchemeColors(Color.parseColor(Constants.RED), Color.parseColor(Constants.ORANGE), Color.parseColor(Constants.YELLOW), Color.parseColor(Constants.GREEN), Color.parseColor(Constants.BLUE), Color.parseColor(Constants.INDIGO), Color.parseColor(Constants.VIOLET));
        personSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                personSwipeRefresh.setRefreshing(false);
                drawThePersonView();
            }
        });
    }

    private void drawThePersonView(){
        DatabaseHelper myDBHelper = new DatabaseHelper(getActivity());

        Cursor personCursor = myDBHelper.getPersonsCursor();
        String[] fromColumns = {"_id","studentID","firstname","lastname","object_id"};
        int[] toViews = {R.id.person_number_textview, R.id.person_id_textview};
        FragmentManager fragMan = this.getFragmentManager();
        CustomSimpleCursorAdapter mySimpleCursorAdapter = new CustomSimpleCursorAdapter(this, fragMan, getActivity(), R.layout.person_layout, personCursor, fromColumns, toViews, 0);

        // Replace the _id column with a person count
        mySimpleCursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                String counter = Integer.toString((cursor.getPosition()+1));
                TextView modifiedTextView = (TextView) view;
                if(columnIndex == 0){
                    modifiedTextView.setText(counter);
                    return true;
                }
                return false;
            }
        });

        ListView myListView = (ListView) rootView.findViewById(R.id.person_row);

        // Draw the list
        myListView.setAdapter(mySimpleCursorAdapter);
    }

    // Pass me a tab index (see Constants.java) and I'll return a refrence to that tab.
    private String getFragmentTag(int tagID){
        return "android:switcher:" + R.id.pager + ":" + tagID;
    }

    @Override
    public void changeTab(int tab) {
        myViewPager.setCurrentItem(tab);
    }
}

CustomSimpleCursorAdapter.java

public class CustomSimpleCursorAdapter extends SimpleCursorAdapter {

    private Context context;
    private Cursor cursor;

    public CustomSimpleCursorAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to, int flags) {
        super(context, layout, cursor, from, to, flags);
        this.context = context;
        this.cursor = cursor;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent){

        // Set alternating rows to different colors.
        View row = super.getView(position, convertView, parent);
        if(position % 2 == 0){
            row.setBackgroundColor(Color.parseColor(Constants.WHITE));
        } else {
            row.setBackgroundColor(Color.parseColor(Constants.LIGHTGREY));
        }

        // When the delete icon is clicked, delete from the database.
        // Then, refresh the view.
        ImageView deleteButton = (ImageView) row.findViewById(R.id.person_delete_button);
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cursor.moveToPosition(position);
                final String personID = cursor.getString(1);

                Toast statusToast = Toast.makeText(context, "Deleting person " + personID + " ...", Toast.LENGTH_SHORT);
                statusToast.show();

                DatabaseHelper myDBHelper = new DatabaseHelper(context);
                myDBHelper.deletePerson(personID);

            }
        });

        return row;
    }
}

Solution

  • I found this Stack Overflow post that did the trick for me. I am posting the answer I used in case it might help somebody else in the future.