我写了一个Baseadapter,然后listview继承这个Baseadapter,由于布局问题,listview在xml中只显示了5个item
当我的listview行数多于5行时,点击大于5行的行时(像我有6行数据,点击第六行),程序就会崩溃掉,错误提示是一个空指针
然后我尝试了去改变item的个数,每当我的数据多于这个item个数时,点击就会出现错误
public void onItemClick(AdapterView< ? > arg0, View v, final int position,
long id) {
// TODO Auto-generated method stub
View view1 = lx.getChildAt(position);
Log.i("aaaaaaaaaaaaaa", ""+position);
MyAdapter.ViewHolder viewHolder = (MyAdapter.ViewHolder) view1.getTag();
viewHolder.btn.setVisibility(View.VISIBLE);
viewHolder.btn.setFocusable(true);
viewHolder.btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
View view1 = lx.getChildAt(position);
MyAdapter.ViewHolder viewHolder = (MyAdapter.ViewHolder) view1
.getTag();
// int a = position;
String name = viewHolder.tx1.getText().toString();
// TODO Auto-generated method stub
SQLiteDatabase db = helper.getWritableDatabase();
db = helper.getWritableDatabase();
db.execSQL("delete from " + Constant.Table_Name
+ " where name=?", new String[] { name });
db.close();
list.remove(position);
lx.setAdapter(new MyAdapter(UserActivity.this, list));
}
});
报错代码是:MyAdapter.ViewHolder viewHolder = (MyAdapter.ViewHolder) view1
.getTag();
说是空指针
回复讨论(解决方案)
listview继承这个Baseadapter?
===================================
先去找找相关的案例看看是怎么实现的吧!!! 可能我说的不清楚,功能我是实现了,现在的问题的 我点击item时,item会把隐藏的按钮显示出来,点击按钮就删除这一行数据
问题是 当我数据超过5个的时候(在布局文件中,listview只显示5个item),点击第五个以后的item程序就会崩溃 View view1 = lx.getChildAt(position);
= >
View view1 = v;
View view1 = lx.getChildAt(position);
= >
View view1 = v;
非常感谢,像你说的改了之后就可以了