Android:将gridview的选择模式重置为默认值

时间:2013-11-28 07:23:30

标签: android gridview baseadapter

我使用以下代码以编程方式显示与CAB相关联的GridView

fileDirGridView.setMultiChoiceModeListener(gridViewMultiChoiceModeListener);
fileDirGridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
fileDirGridView.setItemChecked(position, true);

现在我想要解散multiChoiceModeListener并将一切恢复到以前的状态。为此,我使用以下代码,它总是导致StackOverFull错误。

        @Override
        public void onDestroyActionMode(ActionMode arg0) {
            // TODO Auto-generated method stub
            fileDirGridView.setChoiceMode(GridView.CHOICE_MODE_NONE);

        } 

Logcat

11-28 07:26:31.780: E/AndroidRuntime(14875): FATAL EXCEPTION: main
11-28 07:26:31.780: E/AndroidRuntime(14875): java.lang.StackOverflowError
11-28 07:26:31.780: E/AndroidRuntime(14875):    at android.widget.AbsListView$MultiChoiceModeWrapper.onDestroyActionMode(AbsListView.java:6027)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onDestroyActionMode(PhoneWindow.java:2562)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.android.internal.app.ActionBarImpl$ActionModeImpl.finish(ActionBarImpl.java:846)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at android.widget.AbsListView.setChoiceMode(AbsListView.java:1150)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.example.androidfilemanager.MainActivity$7.onDestroyActionMode(MainActivity.java:465)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at android.widget.AbsListView$MultiChoiceModeWrapper.onDestroyActionMode(AbsListView.java:6027)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onDestroyActionMode(PhoneWindow.java:2562)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.android.internal.app.ActionBarImpl$ActionModeImpl.finish(ActionBarImpl.java:846)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at android.widget.AbsListView.setChoiceMode(AbsListView.java:1150)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.example.androidfilemanager.MainActivity$7.onDestroyActionMode(MainActivity.java:465)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at android.widget.AbsListView$MultiChoiceModeWrapper.onDestroyActionMode(AbsListView.java:6027)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onDestroyActionMode(PhoneWindow.java:2562)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.android.internal.app.ActionBarImpl$ActionModeImpl.finish(ActionBarImpl.java:846)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at android.widget.AbsListView.setChoiceMode(AbsListView.java:1150)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.example.androidfilemanager.MainActivity$7.onDestroyActionMode(MainActivity.java:465)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at android.widget.AbsListView$MultiChoiceModeWrapper.onDestroyActionMode(AbsListView.java:6027)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onDestroyActionMode(PhoneWindow.java:2562)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.android.internal.app.ActionBarImpl$ActionModeImpl.finish(ActionBarImpl.java:846)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at android.widget.AbsListView.setChoiceMode(AbsListView.java:1150)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.example.androidfilemanager.MainActivity$7.onDestroyActionMode(MainActivity.java:465)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at android.widget.AbsListView$MultiChoiceModeWrapper.onDestroyActionMode(AbsListView.java:6027)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onDestroyActionMode(PhoneWindow.java:2562)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.android.internal.app.ActionBarImpl$ActionModeImpl.finish(ActionBarImpl.java:846)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at android.widget.AbsListView.setChoiceMode(AbsListView.java:1150)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.example.androidfilemanager.MainActivity$7.onDestroyActionMode(MainActivity.java:465)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at android.widget.AbsListView$MultiChoiceModeWrapper.onDestroyActionMode(AbsListView.java:6027)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onDestroyActionMode(PhoneWindow.java:2562)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.android.internal.app.ActionBarImpl$ActionModeImpl.finish(ActionBarImpl.java:846)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at android.widget.AbsListView.setChoiceMode(AbsListView.java:1150)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.example.androidfilemanager.MainActivity$7.onDestroyActionMode(MainActivity.java:465)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at android.widget.AbsListView$MultiChoiceModeWrapper.onDestroyActionMode(AbsListView.java:6027)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onDestroyActionMode(PhoneWindow.java:2562)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.android.internal.app.ActionBarImpl$ActionModeImpl.finish(ActionBarImpl.java:846)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at android.widget.AbsListView.setChoiceMode(AbsListView.java:1150)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.example.androidfilemanager.MainActivity$7.onDestroyActionMode(MainActivity.java:465)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at android.widget.AbsListView$MultiChoiceModeWrapper.onDestroyActionMode(AbsListView.java:6027)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onDestroyActionMode(PhoneWindow.java:2562)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.android.internal.app.ActionBarImpl$ActionModeImpl.finish(ActionBarImpl.java:846)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at android.widget.AbsListView.setChoiceMode(AbsListView.java:1150)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.example.androidfilemanager.MainActivity$7.onDestroyActionMode(MainActivity.java:465)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at android.widget.AbsListView$MultiChoiceModeWrapper.onDestroyActionMode(AbsListView.java:6027)
11-28 07:26:31.780: E/AndroidRuntime(14875):    at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onDestroyActionMode(PhoneW

1 个答案:

答案 0 :(得分:2)

我在choiceMode内设置了onDestroyActionMode(),导致stackoverflow错误。所以我刚刚在同一个方法中启动了一个新线程,并在创建的线程中重置了choiceMode

@Override
        public void onDestroyActionMode(ActionMode mode) {
            // TODO Auto-generated method stub
            Thread thread = new Thread()
            {
                @Override
                public void run() {
                    fileDirGridView.clearChoices();
                    fileDirGridView.setChoiceMode(GridView.CHOICE_MODE_NONE);    
                }
            };

            thread.start();
        }