1

我正在使用 aListView来显示播放列表,并使用可点击ImageView来停止/恢复播放,目前我正在编写一个自定义适配器。问题:当我点击 时ImageView,尽管在 LogCat 输出中可以看到点击事件被检测到,但什么也没有发生。做错了什么?适配器代码如下

       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;
}

  }
4

2 回答 2

0

setImageRessource()通过使用而不是setBackgroundResource()无法解释解决了这个问题,但它现在可以工作了

于 2013-02-26T18:01:25.820 回答
0

您可以使用 adaptername.notifyDatasetchanged 方法刷新列表。但不幸的是,这也不起作用,因为当单击事件将被触发时,列表无法知道单击了哪个视图。您可以尝试使用项目点击侦听器,它会为您提供点击项目的位置

于 2013-02-26T18:02:54.250 回答