samedi 7 novembre 2015

Contextual Action Bar unable to delete selected item from SQLite

CAB unable to delete the data from cursor adapter

public class History extends ListActivity{

private ReminderDB mDbHelper;
List selections = new ArrayList();
int count = 0;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.history);

    mDbHelper = new ReminderDB(this);
    mDbHelper.open();
    fillData();
    registerForContextMenu(getListView());
}

private void fillData() {
    final Cursor remindersCursor = mDbHelper.fetchAllHistoryEvents();
    startManagingCursor(remindersCursor);
    // an array for the fields to show in the list row (now only the TITLE)
    String[] from = new String[]{ReminderDB.KEY_TITLE};
    // and an array of the fields for each list row
    int[] to = new int[]{R.id.row1};
    // a simple cursor adapter and set it to display
    final HistoryCustomCursorAdapter history = new HistoryCustomCursorAdapter(this, R.layout.history_list_row, remindersCursor, from, to);
    setListAdapter(history);
    getListView().setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
    getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
            if(checked){
                selections.add(history.getItem(position));
                count++;
                mode.setTitle(count+ " Selected");
            }else{
                selections.remove(history.getItem(position));
                count--;
                mode.setTitle(count+ " Selected");
            }
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater menuInflater = getMenuInflater();
            menuInflater.inflate(R.menu.contextual_menu, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            if (item.getItemId()==R.id.item_delete){
                AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
                mDbHelper.deleteHistory(info.id);
                history.notifyDataSetChanged();
                mode.finish();
            }
            fillData();
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            count = 0;
            selections.clear();
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    fillData();
}

}

This is the error message i get

11-08 14:24:03.475 6934-6934/com.example.windchin.digitalbrain E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException at com.example.windchin.digitalbrain.History$1.onActionItemClicked(History.java:83) at android.widget.AbsListView$MultiChoiceModeWrapper.onActionItemClicked(AbsListView.java:7680) at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onActionItemClicked(PhoneWindow.java:3299) at com.android.internal.app.ActionBarImpl$ActionModeImpl.onMenuItemSelected(ActionBarImpl.java:1009) at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735) at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152) at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874) at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:630) at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:200) at android.view.View.performClick(View.java:4475) at android.view.View$PerformClick.run(View.java:18786) at android.os.Handler.handleCallback(Handler.java:730) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5419) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) at dalvik.system.NativeStart.main(Native Method)

Aucun commentaire:

Enregistrer un commentaire