0

I put a button on a each row of a ListView and now I want to get the position (number of the row) of that button and show it using TOAST when some one press that button, not the row.

I don't know how to do that, I can't even show the Toast, here is my code:

        ListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> list, View view, int position, long id) {
            Log.i(TAG, "onListItemClick: " + position);

            Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();            

            }
    });

I'm learnign how to develope for android and I'm not an english native speaker, Thanks for your help!

Jacobo
  • 87
  • 1
  • 1
  • 13
  • You can take a look at [this](http://stackoverflow.com/questions/1709166/android-listview-elements-with-multiple-clickable-buttons?rq=1) thread.. – Shashank Kadne Aug 31 '13 at 15:18

4 Answers4

1

Shortest way list.getPositionForView(view);

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
0

In your Adaper write the setOnclickListner for a particular button. Here you can able to get location.

public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    // TODO Auto-generated method stub
    final InviteOddViewHolder holderodd;

    int odd_bg = R.drawable.listing_odd;
    int even_bg = R.drawable.listing_even;

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.bookmark_list_item, null);
        holderodd = new InviteOddViewHolder();
        holderodd.button = (Button) convertView
                .findViewById(R.id.button);

        holderodd.button.setOnClickListener(new OnClickListener() {           

        @Override
       public void onClick(View v) 
        {
        Toast.makeText(this, "Posit", Toast.LENGTH_LONG).show();
         }    
       });
        convertView.setTag(holderodd);

    } else {
        holderodd = (InviteOddViewHolder) convertView.getTag();

    }
    return convertView;
}

class InviteOddViewHolder {
    public Button button;

}
Murali Ganesan
  • 2,925
  • 4
  • 20
  • 31
0
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub

    String selection = l.getItemAtPosition(position).toString();
    Toast.makeText(this.getActivity(), selection, Toast.LENGTH_LONG).show();
}
Mohammed
  • 13
  • 3
  • Please try to avoid just dumping code as an answer and try to explain what it does and why. Your code might not be obvious for people who do not have the relevant coding experience. – Frits Aug 08 '16 at 14:32
  • Confirm your answer before posting here, whether it will work or not. Please don't waste the developers time but trying unless code. – Rajesh Rajaram Aug 08 '16 at 16:34
  • it is working fine. I won't waste anyone's times here. – Mohammed Aug 09 '16 at 13:41
0

For your information, whenever there is/are Clickable elements like Buttons or ImageButtons present in your ListView element, they take the control of click events. And your ListView won't get the chance to accept the click event.

So, onListItemClick(...) is useless here.

What you simply have to do is, write setOnClickListener for your button on your adapter class and get the selected position of your ListView.

Rajesh Rajaram
  • 3,271
  • 4
  • 29
  • 48