0

Currently I have

<ListView>
<TextView>
</ListView>

and the following method works perfectly

@Override
protected void onListItemClick(ListView l, View v, int position, long id)

I'm planning to change the layout to

<ListView>
<TextView>
<Button>
</ListView>

public void buttonListener(View v)

Is there a simple way I can retrieve the ListView l and int position from inside the button listener?

Thank you!

Silla Tan
  • 15
  • 3
  • 2
    Your question and your code,sound basic please look at some basic tutorials for making a custom listview.. – user370305 Dec 17 '11 at 10:47
  • Thank you for your comment. I've been looking through most tutorials and all of the ones I've come across use onListItemClick as the solution. None really display how it could be done with a Button inside a ListItem. – Silla Tan Dec 17 '11 at 11:13

6 Answers6

2

Declare your button listener inside

@Override
public View getView(int position, View convertView, ViewGroup parent)

of your list adapter.

There you can access the position and the listview.

Noureddine AMRI
  • 2,942
  • 1
  • 21
  • 28
0

In your adapter give the button a tag with the position Next findViewById(button), and getTag() to find out the position

Don't know if it is the best way, but it'll work!

By the way, read up on listitems with buttons, it carries some problems with the onClickListeners

vanleeuwenbram
  • 1,289
  • 11
  • 19
0

You can add an id to the list view (myView for example) and from the button listener, you get the list view object and get all its properties.


See the below code:

ListView listView= (ListView ) YourPage.findViewById(R.id.myView );
Reno
  • 33,594
  • 11
  • 89
  • 102
Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
0
listview.setOnItemClickListener(new ListView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

            //here arg2 is the position of particular listview item


            }
        });
sai
  • 2,562
  • 8
  • 31
  • 46
0

i think you need a custome listview in which you can click on costume button and get position and base on that you can continue with requirement.

Sai geetha Listview

Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
0
  YTou should try this code:

   @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    // Get the item that was clicked
    Object o = this.getListAdapter().getItem(position);
    String keyword = o.toString();
    Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_SHORT)
            .show();

 }
});

If you wanna more see just like you problems click below links

Android ListView: get data index of visible item

http://www.androidpeople.com/android-listview-onclick

http://developer.android.com/reference/android/widget/ListView.html

Community
  • 1
  • 1
Horrorgoogle
  • 7,858
  • 11
  • 48
  • 81
  • that's exactly what I have right now and it works. i've create a button inside each listview item. the problem is i don't have access to the listview l and int position from within the button listener... sorry if i didn't word it very well – Silla Tan Dec 17 '11 at 10:58