如何在我的代码中正确使用 notifyDataSetChanged?

How do I properly use notifyDataSetChanged in my code?

我正在使用自定义 SimpleCursorAdapter 填充 ListView。

我有一个删除功能,可以从支持 ListView 的数据库中删除一个人。

我不知道该放在哪里 notifyDataSetChanged() 以便我的列表刷新以显示此人已被删除。

我正在使用 SwipeRefreshLayout,如果我滑动它会显示该人已被删除,但我更希望列表在该人被删除时自动更新。

我应该在我的代码中的什么地方使用 notifyDataSetChanged()

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;
    }
}

引用 Android 文档:

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

因此,无论何时更改数据,都可以调用此方法来通知视图,数据集已更改,应该重新绘制。

有关详细信息,请参阅此 link

我发现 Stack Overflow post 对我有用。我正在 post我使用的答案,以防将来它可能对其他人有所帮助。