我正在尝试让listView拥有一个与listView上每个项目对应的按钮。例如,如果我在列表中有产品,我想单击按钮并在单击按钮时显示该特定产品的信息。如何在适配器中为我的按钮添加一个on click侦听器,以便它根据listview中的每个项目工作?
这是我的自定义数组适配器。
public class MyAdapter extends BaseAdapter {
private Context mContext;
private List<Bean> mList;
public MyAdapter(Context context,List<Bean> list){
mContext=context;
mList=list;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
//use convertView recycle
if(convertView==null){
holder=new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.content_orders, parent, false);
holder.textView= (TextView) convertView.findViewById(R.id.textView2);
holder.imageView= (ImageView) convertView.findViewById(R.id.imageView2);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
//set text and url
holder.textView.setText(mList.get(position).getText());
Picasso.with(mContext).load(mList.get(position).getUrl()).resize(500,500).into(holder.imageView);
return convertView;
}
class ViewHolder{
TextView textView;
ImageView imageView;
}
}
这是我的按钮
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Information"
android:id="@+id/button5"
android:layout_below="@+id/button4"
android:layout_alignRight="@+id/button4"
android:layout_alignEnd="@+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
答案 0 :(得分:1)
使用Textview和ImageView将按钮视图放入行布局。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
//use convertView recycle
if(convertView==null){
holder=new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.content_orders, parent, false);
holder.textView= (TextView) convertView.findViewById(R.id.textView2);
holder.imageView= (ImageView) convertView.findViewById(R.id.imageView2);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.clickableButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Code goes here
}
});
//set text and url
holder.textView.setText(mList.get(position).getText());
Picasso.with(mContext).load(mList.get(position).getUrl()).resize(500,500).into(holder.imageView);
return convertView;
}
答案 1 :(得分:1)
您可以将点击监听器添加到getView()
方法中的按钮。然后你可以使用对'持有者&#39;的引用。自定义所需的列表项特定操作。
答案 2 :(得分:0)
如果您提供的按钮xml代码位于content_orders布局中。然后代码应该是这样的
public class MyAdapter extends BaseAdapter {
private Context mContext;
private List<Bean> mList;
public MyAdapter(Context context,List<Bean> list){
mContext=context;
mList=list;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
//use convertView recycle
if(convertView==null){
holder=new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.content_orders, parent, false);
holder.textView= (TextView) convertView.findViewById(R.id.textView2);
holder.imageView= (ImageView) convertView.findViewById(R.id.imageView2);
holder.btn= (Button) convertView.findViewById(R.id.button5);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
//set text and url
holder.textView.setText(mList.get(position).getText());
Picasso.with(mContext).load(mList.get(position).getUrl()).resize(500,500).into(holder.imageView);
holder.btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something
Toast.makeText(mContext,String.valueOf(position),Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
class ViewHolder{
TextView textView;
ImageView imageView;
Button btn;
}
}