0

Here is some of the XML of a Button that is on every cell in my ListView:

android:onClick="buttonTouched"

And this is what is called on my ListActivity:

public void buttonTouched(View v) 
{
    // how do I get the "position" of the cell this view is on?
}

How do I get the cell position of the button that was touched?

Ethan Allen
  • 14,425
  • 24
  • 101
  • 194

3 Answers3

0

In your Adapter try this:

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    View row = convertView;
    YourWrapper wrapper = null;

    if (row == null)
    {
        row = inflater.inflate(R.layout.layout, parent, false);
        wrapper = new YourWrapper (row);
        row.setTag(wrapper);
    }
    else
        wrapper = (YourWrapper) row.getTag();

    wrapper.getButton().setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // What you want
        }
    });

    return row;
}
Artyom Kiriliyk
  • 2,513
  • 1
  • 17
  • 21
0

Since I had a number of buttons on the same row, I ended up just setting a tags in my adapter to the positions needed and then getting the tag on button touch:

public void imageClick(View v) 
{
    int position = (Integer) v.getTag();
}

The reason I went this route is because I needed to have the click done in the ListActivity to access some data I needed there that wasn't in the adapter.

Ethan Allen
  • 14,425
  • 24
  • 101
  • 194
0

Please take a look at this: OnItemClickListener. Implementation Example: setOnItemClickListener on custom ListView.

Since you have 3 buttons in each row of the list view and you have 3 diff implementation for them, there is another way of doing this.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    if(null == convertView){
        holder = new ViewHolder();
        convertView = inflater.inflate(LAYOUT_RESOURCE, null);
        holder.btn1 = (Button) convertView.findViewById(R.id.btn1);
        holder.btn2 = (Button) convertView.findViewById(R.id.btn2);
        holder.btn3 = (Button) convertView.findViewById(R.id.btn3);
        //setting tag to reduce hierarchy lookup
        convertView.setTag(holder);
    } else {
        //getting tag to obtain the already found views
        holder = (ViewHolder) convertView.getTag();
    }
    //Updating fields
    holder.btn1.setOnClickListener(commonListener); 
    holder.btn2.setOnClickListener(commonListener); 
    holder.btn3.setOnClickListener(commonListener); 
    //returning the populated view
    return convertView;
}

OnClickListener commonListener = new OnClickListener(){

    @Override
        public void onClick(View v) {
            switch(v.getId()){
                case R.id.btn1:
                //do your work
                break;
            case R.id.btn2:
                //do your work
                break;
                case R.id.btn3:
                //do your work
                break;
                default:
                break;
            }
        }
    }); 
/**
 * Holder class to improve performance. Helps in reducing the lookup for
 * views.
 * 
 */
private static class ViewHolder {

    Button btn1;
    Button btn2;
    Button btn3;
}

Hope this helps.

Community
  • 1
  • 1
Abhishek Nandi
  • 4,265
  • 1
  • 30
  • 43