简体   繁体   English

上下文操作栏无法从SQLite删除所选项目

[英]Contextual Action Bar unable to delete selected item from SQLite

CAB unable to delete the data from cursor adapter CAB无法从游标适配器删除数据

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 getting: 这是我收到的错误消息:

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.perf 11-08 14:24:03.475 6934-6934 / com.example.windchin.digitalbrain E / AndroidRuntime致命异常:com.example.windchin.digitalbrain.History $ 1.onActionItemClicked(History.java:main java.lang.NullPointerException 83)位于com.android.internal.policy.impl.PhoneWindow $ DecorView $ ActionModeCallbackWrapper.onActionItemClicked(PhoneWindow.java:3299)位于android.widget.AbsListView $ MultiChoiceModeWrapper.onActionItemClicked(AbsListView.java:7680)在com.android.internal .com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)上的.app.ActionBarImpl $ ActionModeImpl.onMenuItemSelected(ActionBarImpl.java:1009)在com.android.internal.view.menu.MenuItemImpl.invoke (MenuItemImpl.java:152)在com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)在com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:630)在com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:200)位于android.view.View.perf ormClick(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) android.os.Handler.dispatchMessage(Handler)上的ormClick(View.java:4475)android.view.View $ PerformClick.run(View.java:18786)android.os.Handler.handleCallback(Handler.java:730) .java:92),位于android.os.Looper.loop(Looper.java:176),位于android.app.ActivityThread.main(ActivityThread.java:5419),位于java.lang.reflect.Method.invokeNative(Native Method) com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1046)处的com.android.internal.os.ZygoteInit.main(java.lang.reflect.Method.invoke(Method.java:525) ZygoteInit.java:862),位于dalvik.system.NativeStart.main(本机方法)

This is cursor adapter class 这是游标适配器类

public class HistoryCustomCursorAdapter extends SimpleCursorAdapter {
    private Context context;
    private ReminderDB mDbHelper;
    private int layout;

    public HistoryCustomCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.context = context;
        this.layout = layout;
        mDbHelper = new ReminderDB(context);
        mDbHelper.open();
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        final LayoutInflater inflater = LayoutInflater.from(context);
        View rowView = inflater.inflate(layout, parent, false);

        int titleCol = cursor.getColumnIndex(ReminderDB.KEY_TITLE);
        String title = cursor.getString(titleCol);
        TextView textView1 = (TextView) rowView.findViewById(R.id.row1);
        TextView textView2 = (TextView) rowView.findViewById(R.id.row2);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);

        textView1.setText(title);

        String Hdate = cursor.getString(cursor.getColumnIndex(ReminderDB.KEY_DATE_TIME));
        String latitude = cursor.getString(cursor.getColumnIndex(ReminderDB.KEY_LATITUDE));
        String longitude = cursor.getString(cursor.getColumnIndex(ReminderDB.KEY_LONGITUDE));

        if(Hdate != null)
            imageView.setImageResource(R.drawable.cclock);
        else
            imageView.setImageResource(R.drawable.maps);

        if(Hdate != null)
        {
            textView2.setText(Hdate);
        }
        else
        {
            Cursor c = mDbHelper.fetchLocation(latitude, longitude);
            String location = c.getString(c.getColumnIndex(ReminderDB.KEY_LOCATION));
            int option = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ReminderDB.KEY_LOCATIONOPTION)));
            String locationOption;
            if (option == 0){
                locationOption = "When I Arrive";
            } else {
                locationOption = "When I Leave";
            }
            textView2.setText(locationOption + ": " + location);
        }
        return rowView;
    }
}

Based on your comment, then you have a problem at these lines: 根据您的评论,那么您在以下几行有问题:

            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
            mDbHelper.deleteHistory(info.id);

This means that your info object is null and your call to item.getMenuInfo() is not working. 这意味着您的info对象为null并且对item.getMenuInfo()调用不起作用。

That means your HistoryCustomCursorAdapter is not setting this value correctly. 这意味着您的HistoryCustomCursorAdapter没有正确设置此值。 You have not posted the code for that object, but I think this should be sufficient to allow you to find the error. 您尚未发布该对象的代码,但是我认为这应该足以使您发现错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM