2013-02-26 208 views
1

我正在使用ListView來顯示播放列表,我使用可點擊的ImageView來停止/恢復播放,目前我正在編寫自定義適配器。問題是:當我點擊ImageView時,雖然檢測到點擊事件,但我沒有發生任何事情,因爲我可以在LogCat輸出中看到。什麼做錯了?下面android-在ListView中處理項目點擊

 public class AudioListAdapter extends ArrayAdapter<Playlist> { 

Context context; 
int layoutResourceId; 
Playlist data[] = null; 


public AudioListAdapter(Context context, int layoutResourceId, 
     Playlist[] data) { 
    super(context, layoutResourceId, data); 
    this.layoutResourceId = layoutResourceId; 
    this.context = context; 
    this.data = data; 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View row = convertView; 
    PlaylistHolder holder = null; 

    if (row == null) { 
     LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
     row = inflater.inflate(layoutResourceId, parent, false); 

     holder = new PlaylistHolder(); 
     PlaylistHolder.imgBtnPlay = (ImageView) row 
       .findViewById(R.id.imgListBtnPlay); 
     holder.txtTitle = (TextView) row.findViewById(R.id.tvListItemSong); 
     holder.txtArtist = (TextView) row 
       .findViewById(R.id.tvListItemArtist); 

     Playlist playlist = data[position]; 
     holder.txtTitle.setText(playlist.Title); 
     holder.txtArtist.setText(playlist.Artist); 
     PlaylistHolder.imgBtnPlay.setImageResource(playlist.btn); 
     PlaylistHolder.imgBtnPlay 
       .setOnClickListener(new View.OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         PlaylistHolder.imgBtnPlay 
           .setBackgroundResource(R.drawable.list_button_pause); 
         Log.e("onClick", "image clicked"); //Click events are detected 

        } 
       }); 

     row.setTag(holder); 
    } else { 
     holder = (PlaylistHolder) row.getTag(); 
    } 

    return row; 
} 

static class PlaylistHolder { 
    static ImageView imgBtnPlay; 
    TextView txtTitle, txtArtist; 
} 

    } 
+0

@ Mover1ck你剛剛改變了圖像點擊按鈕....是不是會發生..? – Pragnani 2013-02-26 17:49:23

+0

按鈕的背景是否在點擊該項目時發生了變化? – Elior 2013-02-26 17:56:28

+0

就是這個問題。儘管點擊事件被系統檢測到,但按鈕背景不會改變 – Droidman 2013-02-26 17:57:01

回答

0

適配器代碼使用的setImageRessource()代替setBackgroundResource()無法解釋,解決了這個問題,但現在的工作

0

可不幸的是使用adaptername.notifyDatasetchanged method..But刷新列表,也將不工作,因爲當單擊事件將被解僱名單不能知道哪個視圖被點擊。或者你可以嘗試使用項目點擊監聽器,這將提供你點擊的項目的位置

相關問題