listView.getSelectedItemPosition()的返回指数-1 [英] listView.getSelectedItemPosition() return index-1
本文介绍了listView.getSelectedItemPosition()的返回指数-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我的自定义列表视图,并在每一行结束时,我必须要的ImageView从列表中删除该行,但是当我点击这个图片上我得到Arryindesoutofboundexception:长度= 681 indez = -1
帮助我
私人OnClickListener imageviewClickListener =新OnClickListener(){
@覆盖
公共无效的onClick(视图v)
{
INT指数;
指数= listView.getSelectedItemPosition(); // itemsListView是列表视图
dataAdapter.remove(topicsList.get(指数));
topicsList.clear();
dataAdapter.notifyDataSetChanged();
}
};
未选中解决方案
您的项目,因为图像拦截触摸事件,因此所选择的位置是-1。
为了使这项工作,你需要告诉OnClickListener它属于什么项目:
私有静态类MyClickListener实现OnClickListener {
私人最终诠释MINDEX; 私人MyClickListener(INT指数){
MINDEX =指数;
} @覆盖
公共无效的onClick(视图v){
dataAdapter.remove(topicsList.get(MINDEX));
topicsList.clear();
dataAdapter.notifyDataSetChanged();
}
}
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=681 indez=-1"
Help me
private OnClickListener imageviewClickListener = new OnClickListener() {
@Override
public void onClick(View v)
{
int index;
index=listView.getSelectedItemPosition();//itemsListView is the listview
dataAdapter.remove(topicsList.get(index));
topicsList.clear();
dataAdapter.notifyDataSetChanged();
}
};
解决方案
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:
private static class MyClickListener implements OnClickListener {
private final int mIndex;
private MyClickListener (int index) {
mIndex = index;
}
@Override
public void onClick(View v) {
dataAdapter.remove(topicsList.get(mIndex));
topicsList.clear();
dataAdapter.notifyDataSetChanged();
}
}
这篇关于listView.getSelectedItemPosition()的返回指数-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文