我曾經在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