1

I have a little problem. I want to create navigation drawer with titles, but eclipse throws me a class cast exception. The problem is in adapter, but I can't find it. Can you please help me. Here is code for my adapter

public class NavDrawerAdapter extends ArrayAdapter<NavDrawerItem>{

private LayoutInflater inflater;

public NavDrawerAdapter(Context context, int textViewResourceId, NavDrawerItem[] objects ) {
    super(context, textViewResourceId, objects);
    this.inflater = LayoutInflater.from(context);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null ;
    NavDrawerItem menuItem = this.getItem(position);
    if ( menuItem.getType() == NavMenuItem.ITEM_TYPE ) {
        view = getItemView(convertView, parent, menuItem );
    }
    else {
        view = getSectionView(convertView, parent, menuItem);
    }
    return view ;
}

public View getItemView( View convertView, ViewGroup parentView, NavDrawerItem navDrawerItem ) {

    NavMenuItem menuItem = (NavMenuItem) navDrawerItem ;
    NavMenuItemHolder navMenuItemHolder = null;

    if (convertView == null) {
        convertView = inflater.inflate( R.layout.navdrawer_item, parentView, false);
        TextView labelView = (TextView) convertView
                .findViewById( R.id.navmenuitem_label );
        ImageView i = (ImageView) convertView.findViewById(R.id.navmenuitem_icon);

        navMenuItemHolder = new NavMenuItemHolder();
        navMenuItemHolder.labelView = labelView ;
        navMenuItemHolder.image = i;

        convertView.setTag(navMenuItemHolder);
    }

    if ( navMenuItemHolder == null ) {
        navMenuItemHolder = (NavMenuItemHolder) convertView.getTag();
    }

    navMenuItemHolder.labelView.setText(menuItem.getLabel().toString());

    return convertView ;
}

public View getSectionView(View convertView, ViewGroup parentView,
        NavDrawerItem navDrawerItem) {

    NavMenuSection menuSection = (NavMenuSection) navDrawerItem ;
    NavMenuSectionHolder navMenuItemHolder = null;

    if (convertView == null) {
        convertView = inflater.inflate( R.layout.navdrawer_section, parentView, false);
        TextView labelView = (TextView) convertView
                .findViewById( R.id.navmenusection_label );

        navMenuItemHolder = new NavMenuSectionHolder();
        navMenuItemHolder.labelView = labelView ;
        convertView.setTag(navMenuItemHolder);
    }

    if ( navMenuItemHolder == null ) {
        navMenuItemHolder = (NavMenuSectionHolder) convertView.getTag();
    }

    navMenuItemHolder.labelView.setText(menuSection.getLabel().toString());

    return convertView ;
}

private static class NavMenuItemHolder {
    private TextView labelView;
    private ImageView image;
}

private static class NavMenuSectionHolder {
    private TextView labelView;
}

}

and here is code for my main activity

public class MainActivity extends Activity{

private DrawerLayout drawerLayout;
private NavDrawerAdapter adapter;
private ListView mDrawerList;
private ImageView menu;
private RelativeLayout rightRL;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    menu = (ImageView) findViewById(R.id.imageView1);
    rightRL = (RelativeLayout) findViewById(R.id.whatYouWantInRightDrawerProfil);

    NavDrawerItem[] m = new NavDrawerItem[]{
            NavMenuSection.create(100, "Demos"),
            NavMenuItem.create(101, "kako si", false, this),
            NavMenuSection.create(200, "General"),
            NavMenuItem.create(202, "Rate this app", false, this)
    };
    adapter = new NavDrawerAdapter(this, R.layout.navdrawer_item, m);
    mDrawerList.setAdapter(adapter);


}

public void onRight(View view) {
    drawerLayout.openDrawer(rightRL);
}

}

This is my logcat from eclipse

FATAL EXCEPTION: main java.lang.ClassCastException: com.markoKurt.zeljko.NavDrawerAdapter$NavMenuSectionHolder cannot be cast to com.markoKurt.zeljko.NavDrawerAdapter$NavMenuItemHolder at com.markoKurt.zeljko.NavDrawerAdapter.getItemView(NavDrawerAdapter.java:52) at com.markoKurt.zeljko.NavDrawerAdapter.getView(NavDrawerAdapter.java:25) at android.widget.AbsListView.obtainView(AbsListView.java:2449) at android.widget.ListView.measureHeightOfChildren(ListView.java:1250) at android.widget.ListView.onMeasure(ListView.java:1162) at android.view.View.measure(View.java:15504) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399) at android.view.View.measure(View.java:15504) at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:866) at android.view.View.measure(View.java:15504) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:15504) at android.widget.LinearLayout.measureVertical(LinearLayout.java:833) at android.widget.LinearLayout.onMeasure(LinearLayout.java:574) at android.view.View.measure(View.java:15504) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2385) at android.view.View.measure(View.java:15504) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1979) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1222) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1395) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4526) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) at android.view.Choreographer.doCallbacks(Choreographer.java:555) at android.view.Choreographer.doFrame(Choreographer.java:525) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4921) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) at dalvik.system.NativeStart.main(Native Method)

Please help me, and thanks in advance

5 Answers 5

2

your issue is related to the fact that you get only one null convertView, so the tag content is set just once for NavMenuSectionHolder or NavMenuItemHolder. Usually, when you have to handle more than one convertView you can override getViewTypeCount and getItemViewType. In your case, the two holder classes differ just for ImageView. So I would suggest you to have just one holder class, (inflating one layout), and hiding the ImageView when you don't need it

Sign up to request clarification or add additional context in comments.

Comments

2

I solved the problem. I just needed to add

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    return this.getItem(position).getType();
}

@Override
public boolean isEnabled(int position) {
    return getItem(position).isEnabled();
}

Comments

1

you trying to cast navDrawerItem to navMenuItem

NavMenuItem menuItem = (NavMenuItem) navDrawerItem;

change it with

NavDrawerItem drawerItem = (NavDrawerItem ) navDrawerItem;

and do accordingly..

2 Comments

so i have to change it also in getSectionView(). Insted NavMenuItem menuItem = (NavMenuItem) navDrawerItem;
but that's wrong. The stacktrace says java.lang.ClassCastException: com.markoKurt.zeljko.NavDrawerAdapter$NavMenuSectionHolder cannot be cast to com.markoKurt.zeljko.NavDrawerAdapter$NavMenuItemHolder
1

Try to change the logic for different layout like this, http://www.survivingwithandroid.com/2014/08/android-listview-with-multiple-row.html

Comments

0

In getItemView and getSectionView you can get different views, so you need to check class instance.

public View getSectionView(View convertView, ViewGroup parentView,
NavDrawerItem navDrawerItem) {

NavMenuSection menuSection;
if (navDrawerItem !=null)
if (navDrawerItem instanceof NavMenuSection){
menuSection = navDrawerItem}
else {
menuSection = new NavMenuSection()
}else {
menuSection = new NavMenuSection()
}
public View getItemView( View convertView, ViewGroup parentView, NavDrawerItem navDrawerItem ) {

NavMenuItem menuItem = (NavMenuItem) navDrawerItem ;
    NavMenuItemHolder navMenuItemHolder = null;
NavMenuSection menuSection;
if (menuItem!=null)
if (menuItem instanceof  NavMenuItem){
menuItem =  NavMenuItem
else {
menuItem = new  NavMenuItem()
}else {
menuItem= new  NavMenuItem()
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.