简体   繁体   中英

OnItemLongClick on ExpandableListView crashes when a group is expanded

I am working on an android project and making use of an ExpandableListView with a Custom ExpandableListAdapter.

I have an OnItemLongClickListener the the click handler should only be used when the group header is long pressed, any long clicks on the children should be ignored.

When all of the group headers are collapsed, the code works fine, when any group is expanded, even one I am not long clicking on, causes an exception to be thrown.

The stacktrace is a follows:

7452-7452/com.BoardiesITSolutions.MysqlManager E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.ClassCastException: java.lang.String cannot be cast to com.BoardiesITSolutions.MysqlManager.DatabaseDetails at com.BoardiesITSolutions.MysqlManager.ConnectedDBManagerHost$1.onItemLongClick(ConnectedDBManagerHost.java:142) at android.widget.AbsListView.performLongPress(AbsListView.java:2854) at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:2804) at android.os.Handler.handleCallback(Handler.java:730) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5103) 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:737) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method)

Below is my code

mDrawerDBs.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_GROUP)
            {
                for (int i = 0; i < listAdapter.getGroupCount(); i++)
                {
                    ((DatabaseDetails)parent.getItemAtPosition(i)).setIsDefaultDatabase(false);
                }
                DatabaseDetails dbDetails = (DatabaseDetails)parent.getItemAtPosition(position);
                ((DatabaseDetails) parent.getItemAtPosition(position)).setIsDefaultDatabase(true);

                ConnectionsManager.connectedDatabase.put("database", Encryption.encrypt(((DatabaseDetails) parent.getItemAtPosition(position)).getDatabaseName()));

                QueryEditor queryFragment = (QueryEditor)getSupportFragmentManager().findFragmentById(R.id.fragment_queryEditor);
                queryFragment.setDatabaseUsed();
                listAdapter.notifyDataSetChanged();
            }
            return true;
            }
        });
    }

Thanks for any help you can provide.

The exception tells you exactly what's wrong:

java.lang.ClassCastException: java.lang.String cannot be cast to com.BoardiesITSolutions.MysqlManager.DatabaseDetails

You are trying to cast a String to DatabaseDetails and I guess it happens somewhere here:

((DatabaseDetails)parent.getItemAtPosition(i)).setIsDefaultDatabase(false);

Sometimes the item returned by getItemAtPosition() must be String otherwise this exception wouldn't be thrown. Just debug the click listener step by step until you figure out exactly where and why this Exception occurs and then fix it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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