Skip to content Skip to sidebar Skip to footer

Listview.getselecteditemposition() Return Index-1

I have my custom listview and at the end of each row I have ImageView to delete that row from the list but when I click on this image I get 'Arryindesoutofboundexception: length=68

Solution 1:

Your item isn't selected because the image intercepts the touch event, therefore the selected position is -1. In order to make this work you need to tell the OnClickListener what item it belongs to:

privatestaticclassMyClickListenerimplementsOnClickListener {
    privatefinalint mIndex;

    privateMyClickListener(int index) {
        mIndex = index;
    }

    @OverridepublicvoidonClick(View v) {
        dataAdapter.remove(topicsList.get(mIndex));
        topicsList.clear();
        dataAdapter.notifyDataSetChanged();
    }
}

Solution 2:

You're trying to get position of an selected item from a ListView, even though there was no item selected. From the docs of getSelectedItemPosition(), you can see, that if no item is selected, then it returns the INVALID_POSITION, which is -1.

Post a Comment for "Listview.getselecteditemposition() Return Index-1"