Skip to main content

ArrayIndexOutOfBoundsException with custom Android Adapter for multiple views in ListView



I am attempting to create a custom Adapter for my ListView since each item in the list can have a different view (a link, toggle, or radio group), but when I try to run the Activity that uses the ListView I receive an error and the app stops. The application is targeted for the Android 1.6 platform.





The code:







public class MenuListAdapter extends BaseAdapter {

private static final String LOG_KEY = MenuListAdapter.class.getSimpleName();



protected List<MenuItem> list;

protected Context ctx;

protected LayoutInflater inflater;



public MenuListAdapter(Context context, List<MenuItem> objects) {

this.list = objects;

this.ctx = context;

this.inflater = (LayoutInflater)this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}



@Override

public View getView(int position, View convertView, ViewGroup parent) {

Log.i(LOG_KEY, "Position: " + position + "; convertView = " + convertView + "; parent=" + parent);

MenuItem item = list.get(position);

Log.i(LOG_KEY, "Item=" + item );



if (convertView == null) {

convertView = this.inflater.inflate(item.getLayout(), null);

}



return convertView;

}



@Override

public boolean areAllItemsEnabled() {

return false;

}



@Override

public boolean isEnabled(int position) {

return true;

}



@Override

public int getCount() {

return this.list.size();

}



@Override

public MenuItem getItem(int position) {

return this.list.get(position);

}



@Override

public long getItemId(int position) {

return position;

}



@Override

public int getItemViewType(int position) {

Log.i(LOG_KEY, "getItemViewType: " + this.list.get(position).getLayout());

return this.list.get(position).getLayout();

}



@Override

public int getViewTypeCount() {

Log.i(LOG_KEY, "getViewTypeCount: " + this.list.size());

return this.list.size();

}



}







The error I receive:







java.lang.ArrayIndexOutOfBoundsException

at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:3523)

at android.widget.ListView.measureHeightOfChildren(ListView.java:1158)

at android.widget.ListView.onMeasure(ListView.java:1060)

at android.view.View.measure(View.java:7703)







I do know that the application is returning from getView and everything seems in order.





Any ideas on what could be causing this would be appreciated.





Thanks,





-Dan


Comments

  1. The item view type you are returning from getItemViewType() is >= getViewTypeCount().

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex