Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7613851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:15:15+00:00 2026-05-31T02:15:15+00:00

I got a trivial code involving a ListView and a ArrayAdapter which throws a

  • 0

I got a trivial code involving a ListView and a ArrayAdapter which throws a IndexOutOfBoundsException on some devices. The problem is: I do not know how this Exception occurs, I only get the stacktraces from the Developer Console of Android.

The reduced code example is shown below. How can the getItem operation of the ArrayAdapter fail for the position element? The ArrayAdapter is never changed, there is no other method in the Activity.

I know what a IndexOutOfBoundsException is, I know that I can prevent it by checking the length first. But I’m curious: How can this Exception happen here? How can someone click on a event which does not exist in the datastructure?

Reduced code:

public class EventListActivity extends Activity {
    public void onStart() {
        final ListView listview = new ListView(this);
        final Event[] events = [Retrieve a Array from somewhere]
        final ArrayAdapter<Event> a = new ArrayAdapter<Event>(this, R.layout.eventlistitem, events);
        listview.setAdapter(a);
        listview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Event event = a.getItem(position);
                                ^^^^^^^ throws Exception
            }
        });

Exception:

java.lang.IndexOutOfBoundsException
at java.util.Arrays$ArrayList.get(Arrays.java:75)
at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:298)
at xxx.EventListActivity$3.onItemClick(EventListActivity.java:130)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:15:17+00:00Added an answer on May 31, 2026 at 2:15 am

    Okay, I debugged it, in case someone has the same error, here is the solution:

    If you use ListView.addHeader, the position argument of the onItemClick is shifted by 1. Seems quite weird because the position then has nothing to do with the position in the array-adapter. This SO Question contains the same conclusion, you have to pay attention what you add as headers.

    My question above was in fact not very helpfull because the whole addHeader part was missing, it was impossible to answer the question by others, sorry. Nevertheless, this answer might be helpfull for others.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got some code that is not mine and its producing this warning atm: iehtmlwin.cpp(264)
2013 Edit: async and await now make this trivial! :-) I've got some code
I've got this - possibly trivial - loop/combinations problem similar to binary combinations. I
This seems to be a trivial question but I got hung on it for
I'm not sure exactly how to describe this question, but here goes. I've got
I've got some OpenGL drawing code that I'm trying to optimize. It's currently testing
I got a trivial issue.. and can't solve it. I have this in my
I've got this webapp that needs some memory tuning. While I'm already profiling the
Back in the old days, Help was not trivial but possible: generate some funky
I've got some code that needs to scan through a hierarchical database schema, looking

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.