我曾经在OnListItemClickListener()中试过,但失败了。最后,我对自定义的适配器进行了一些修改,以便使用listview。在getView()中,我将clickListener应用于我经常添加到列表中的项目。 n在那里做所有必需的功能。这里是我的代码,我在列表n中添加图像视图,因此在imageview上应用侦听器。
getChildAt(i)抛出异常,因为它只对可见项-1有效。所以它不起作用。
我认为它会帮助那些想在特定列表项目被选中时改变颜色的人。去吧..
在getView()的定制适配器 // ----------------------------- ---- code ------------------------------------------
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.icon_image_layout, parent, false);
ImageView imageView = (ImageView) rowView.findViewById(R.id.Icon_ImageView);
imageView.setClickable(true);
final int pos=position;
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
try{
if(previous_view!=null)
previous_view.setBackgroundColor(Color.WHITE);
}catch (Exception e) {
System.out.println("Exception Occurs Previous View");
}
v.setBackgroundColor(Color.RED);
MainActivity.imageView.setImageResource(MainActivity.Image_Name[pos]);
previous_view=v;
return false;
}
});
logcat说什么? – Bevor 2010-09-06 13:11:52
写入行后只有空指针异常 – viv 2010-09-06 13:13:07
将代码分成多行。所以我们可以看到_list是否为null,或者getChildAt(1)是否返回null。 – bramp 2010-09-06 13:13:12