0

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" />
2
  • Add Button in ListView rows same way as you have added TextView and ImageView. is there any problem? Commented Jul 12, 2016 at 17:19
  • Add the button to your xml file content_orders, then add it to the custom adapter ViewHolder, and every row in your ListView will include a button. Commented Jul 12, 2016 at 17:22

3 Answers 3

1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Strange this answer is accepted but where you declared clickableButton and bind view holder with it.
1

you can add the click listener to the button in your getView() method. Then you can use references to 'holder' to customize what ever list item specific action you want.

Comments

0

If button xml code that you provided is inside content_orders layout. Then the code should be like this

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;

}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.