you ArrayList size is 1 and you trying to get item 2 from it
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
Question
I have a list item where i have some items in. But no matter what i click, i always get position==-1.
And when i click first position its fine but when i click second or third position my app is crushing and i am getting Logcat:
05-12 19:37:34.131: E/AndroidRuntime(11152): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
05-12 19:37:34.131: E/AndroidRuntime(11152): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
Here is my code onListItemClicked:
class ListItemClicked implements OnItemClickListener
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int position = parent.getSelectedItemPosition();
if(position==-1)
{
Toast.makeText(getApplicationContext(),"first item clicked", Toast.LENGTH_SHORT).show();
}
}
}
Please tell me if you need more info.
Solution
OTHER TIPS
Do not use parent.getSelectedItemPosition()
, but rather use the position
parameter. That's what it's for.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow