注意点:holder = new ViewHolder(convertView)加上参数后还需要写在初始化之后否则报空指针异常。
//音频列表适配器
public class SoundListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<Map<String, String>> data;
public SoundListAdapter(Context context, List<Map<String, String>> datalist) {
this.mInflater = LayoutInflater.from(context);
this.data = datalist;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.sound_list_item, null);
holder = new ViewHolder(convertView);//需写在后面
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.soundId.setText(String.valueOf(position + 1));
holder.soundPath.setText(data.get(position).get("soundpath"));
//button点击事件
//播放
holder.playSound.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return convertView;
}
@OnClick(R.id.play_sound)
void playSound(View v) {
}
}
static class ViewHolder {
@BindView(R.id.sound_id)
TextView soundId;
@BindView(R.id.sound_path)
TextView soundPath;
@BindView(R.id.play_sound)
ImageButton playSound;
ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
最后我发现,注解工具里竟然有view holder的注解快捷方式,竟然一直没发现呼~