从ListView中删除项目后,ListView不会更新

时间:2015-08-04 11:44:10

标签: android listview fragment simplecursoradapter notifydatasetchanged

从lvSimple(ListView)中删除项目后,我无法更新LisView的内容。数据库中的数据将被删除。 ListView的项目也是。但我需要“翻阅”片段才能看到这一点。这是我的代码的一部分。

public class PageFragment extends Fragment {

public boolean onContextItemSelected(MenuItem item) {
    if (item.getItemId() == CM_DELETE_ID) {
        AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        myDB.deleteRow(acmi.id);
        populateListView();
        return true;
    }
    return super.onContextItemSelected(item);
}

public void populateListView() {
    String[] fromFieldNames = new String[]{DBAdapter.KEY_DATE, DBAdapter.KEY_AREA, DBAdapter.KEY_PASSENGER};
    int[] toViewsID = new int[]{R.id.tvItemDate, R.id.tvItemArea, R.id.tvItemPassenger};
    SimpleCursorAdapter myC;
    myC = new SimpleCursorAdapter(view.getContext(), R.layout.item, c, fromFieldNames, toViewsID, 0);
    myC.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            if (columnIndex == 1) {
                String createDate = cursor.getString(columnIndex);
                textView = (TextView) view;
                textView.setText(dateFormat(createDate));
                return true;
            }
            return false;
        }
    });
    lvSimple.setAdapter(myC);
    myC.notifyDataSetChanged();
    lvSimple.invalidateViews();
}
}

0 个答案:

没有答案