public void onItemClick(AdapterView> parent, View view, int position, long id){TextView idTV=(TextView)findViewById(R.id.ID);String DeleteID=idTV.getText().toString();
}
解决办法:
参考网站:blog.chengbo.net/2012/03/09/onitemclick-return-wrong-position-when-listview-has-headerview.html
原因:待续
public void onItemClick(AdapterView> parent, View view, int position, long id){System.out.println("Parameter position:"&#43;position);System.out.println("Parameter id:"&#43;id);if(id<&#61;-1) return ;
}
ListView有Head时&#xff0c;点击Head&#xff0c;发现position参数为0&#xff0c;id为-1&#xff1b;
I/System.out(3287): Parameter position:0
I/System.out(3287): Parameter id:-1
另外&#xff0c;ListView添加head后&#xff0c;一定要对head的onItemClick做处理&#xff0c;否则各种意想不到...
参数id与position的关系&#xff1a;
id&#61;getItemid(position);
官方文档解析&#xff1a;Get the row id associated with the specified position in the list.
发现这里的 row id并不是sqlite中rowid&#xff0c;&#xff08;sqlite中rowid&#xff0c;如果表中有integer主键&#xff0c;rowid的值就等于主键&#xff09;&#xff1b;
那等于啥&#xff1f;等于数据库表中的_id&#xff0c;即getItemid(position)等于选中这行数据中的_id;
数据库中表的定义&#xff1a;CREATE TABLE Books (id integer primary key,author varchar(30),name varchar(30),category varchar(15), _id int);
(默认情况下&#xff0c;SQLite会自动存在一个RowID列&#xff0c;从1开始&#xff0c;每添加一条记录&#43;1 当设置了主键&#xff0c;而且主键的类型为integer时&#xff0c;查询RowID等于主键)