Kekis2014 Kekis2014 - 4 months ago 17
Android Question

Adding button in custom adapter for each item in listView

I'm trying to get a listView to have a button that corresponds to each item on the listView. For example, if I have a product in the list, i want to click the button and display the information for that specific product when i click the button. How can i add an on click listener in the adapter for my button so that it works according to each item in the listview?

This is my custom array adapter.

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;

}
}


And this is my Button

<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" />

Answer

Put the button view into the your row layout with the Textview and 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;
}