我有一张带按钮的表格。我将一个事件处理程序分配给工作表的一个元素。当你得到他的位置。但我需要将一个事件分配给工作表元素中的按钮。我在适配器中执行它并按下它。但是如何确定纸张的哪个元素被按下?
class RecordAdapter extends BaseAdapter implements View.OnClickListener {
MediaPlayer mediaPlayer;
private LayoutInflater inflater;
ArrayList<RecordBean> recordBeans;
Context ctx;
public RecordAdapter(final Context ctx, ArrayList<RecordBean> recordBeans) {
this.recordBeans =recordBeans;
this.ctx=ctx;
inflater = LayoutInflater.from(ctx);
}
@Override
public int getCount() {
return recordBeans.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
if (convertView == null) {
view = inflater.inflate(R.layout.recorditem, parent, false);
holder = new ViewHolder();
holder.date = (TextView) view.findViewById(R.id.recordate);
holder.from = (TextView) view.findViewById(R.id.recordFrom);
holder.to = (TextView) view.findViewById(R.id.recordTo);
holder.seekBar = (SeekBar) view.findViewById(R.id.seekBar);
holder.start = (Button) view.findViewById(R.id.btnStart);
holder.stop = (Button) view.findViewById(R.id.btnStop);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.date.setText(" " + recordBeans.get(position).getDate());
holder.from.setText(" " + recordBeans.get(position).getFrom());
holder.to.setText(" " + recordBeans.get(position).getTo());
holder.start.setOnClickListener(this);
holder.stop.setOnClickListener(this);
return view;
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btnStart:
mediaPlayer = MediaPlayer.create(ctx,Uri.parse(recordBeans.get(3).getFile()));
mediaPlayer.start();
break;
}
}
private static class ViewHolder {
TextView date;
TextView from;
TextView to;
SeekBar seekBar;
Button start;
Button stop;
}
}
}
代替recordBeans.get(3).getFile()
我想写recordBeans.get(itemPosition).getFile()
现在我对所有按钮都有相同的设置
答案 0 :(得分:3)
您可以在getView(...)
方法本身
holder.start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer = MediaPlayer.create(ctx,Uri.parse(recordBeans.get(position).getFile()));
mediaPlayer.start();
}
});
stop
。
holder.stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.stop();
}
});
答案 1 :(得分:2)
请尝试这种方式,希望这有助于您解决问题。
设置 RecordBean 数据以启动按钮 标记,当您点击相应的开始按钮从<获取值< strong>标记并根据您的要求使用它。
class RecordAdapter extends BaseAdapter implements View.OnClickListener {
MediaPlayer mediaPlayer;
ArrayList<RecordBean> recordBeans;
Context ctx;
public RecordAdapter(final Context ctx, ArrayList<RecordBean> recordBeans) {
this.recordBeans =recordBeans;
this.ctx=ctx;
}
@Override
public int getCount() {
return recordBeans.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
if (convertView == null) {
view = LayoutInflater.from(ctx).inflate(R.layout.recorditem, parent, false);
holder = new ViewHolder();
holder.date = (TextView) view.findViewById(R.id.recordate);
holder.from = (TextView) view.findViewById(R.id.recordFrom);
holder.to = (TextView) view.findViewById(R.id.recordTo);
holder.seekBar = (SeekBar) view.findViewById(R.id.seekBar);
holder.start = (Button) view.findViewById(R.id.btnStart);
holder.stop = (Button) view.findViewById(R.id.btnStop);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.date.setText(" " + recordBeans.get(position).getDate());
holder.from.setText(" " + recordBeans.get(position).getFrom());
holder.to.setText(" " + recordBeans.get(position).getTo());
holder.start.setTag(recordBeans.get(position));
holder.start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer = MediaPlayer.create(ctx,Uri.parse(((RecordBean) v.getTag()).getFile()));
mediaPlayer.start();
}
});
holder.stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.stop();
}
});
return view;
}
private static class ViewHolder {
TextView date;
TextView from;
TextView to;
SeekBar seekBar;
Button start;
Button stop;
}
}
答案 2 :(得分:0)
检查一下:
final int itemPosition = position;
holder.start.setOnClickListenerOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer = MediaPlayer.create(ctx,Uri.parse(recordBeans.get(itemPosition ).getFile()));
mediaPlayer.start();
}
});
holder.stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.stop());
}
});