In my android app “Memo English Fruit Master”, I create a customized ListView. In each ListView item, I put a button inside so when user clicks on the button, the app will play a voice over according to the text in that ListView item. The problem is here, how do I know which voice over the app should play. Hence, I need to know the position of the item in ListView. Here are several ways to get listview item position on click.

Set onListItemClick Event Listener on ListView

This is the most simple way to get position of an item within a ListView. In most of the case, users click on an item with a ListView, this event will be triggered and it will call onListItemClick listener by passing four parameters:

  • The ListView where the click happened
  • The view that was clicked within the ListView
  • The position of the view in the list
  • The row id of the item that was clicked

First, we set the listener on the listview.

listView.setOnItemClickListener(onItemClickListener);

Then, we define the listener. Here is the example source code:

private OnItemClickListener onItemClickListener = new OnItemClickListener() {

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		// TODO Auto-generated method stub
		//do your job here, position is the item position in ListView
	}
};

Get ListView Item Position by getPositionForView

The first solution is suitable for user clicking on the ListView item. How about user clicks a button within the ListView item? Let’s say we set the click listener on a button in the getView function which is defined in adapter class.

mybutton.setOnClickListener(myButtonClickListener);

Then we define the onclick listener:

private OnClickListener myButtonClickListener = new OnClickListener() {
	@Override
	public void onClick(View v) {
		View parentRow = (View) v.getParent();
		ListView listView = (ListView) parentRow.getParent();
		final int position = listView.getPositionForView(parentRow);
	}
};

Store Position Data on Button By setTag

Here is another simple way to find the position of item by button click. In the getView function, we can use setTag to set the current view position as tag on the button. In the button click listener, we get the tag of the button and convert it as integer position value.

Here is how we set the tag in getView function:

mybutton.setTag(position);
mybutton.setOnClickListener(myButtonClickListener);

In the button click listener, we will get the tag and find the position value. Here is the source code:

private OnClickListener voiceButtonClickListener = new OnClickListener() {
	@Override
	public void onClick(View v) {
		int position = (Integer) v.getTag();
	}
};
Previous PostNext Post

55 Comments

  1. I want to access the listview from one class to another class.my listview is in fragment.And I have the delete button for listitems in adapter class.How can I get that???

    1. If you can listen the button click and get the row index in the listener, it doesn’t matter where you want to access the listview.

  2. This washed away away code sweat…I was using a popup menu on a listview but it was returning the current position as null.

    Thanks again

  3. Thank you very much brother, you saved my day.
    Thanks a lot. It was really very helpful.

  4. hi i tried the same but im getting this error android.widget.listview cannot be cast to android.widget.LinearLayout any hope how to solve t

    1. Hello, I think the error message tells you the problem very clear. You cannot assign ListView object to LinearLayout reference. Please read my code carefully and you will find the solution.

      Best Regards
      James

  5. i have custom listview with textview and imageview and one button which name is ADD. i want to access list item’s data on other activity on ADD button click. how can get only perticuler list item’s values??

    1. First, you need to get your list item’s data when you click on the ADD button. After you get the data, you can pass the data to your new activity.

  6. thank u very much…sir..its very helpful for me…i have anothr doubt..
    i want to view details from webservices..the details wll come within listview..

  7. I am making a To-Do app. I am using SQLite to store the data from editText View.
    I want to get the id as the position.
    I tried the above code but it throws the nullPointerException to the delete Button.

      1. I done exactly what you say, but the position refers to the id of the SQLite db.
        I want to set the id number as the position, not the position of list view.

        1. Do you know how to bind data into ListView. Actually, you can set the your SQLite id into ListView. Once you find the index of the row, you will find the SQLite id.

  8. sorry, if I want to put a ButtonDownload into a listView to start download the file from getLink, for example, how can I call this?

    Thankyou for any helps

    1. Then, you just put a download button in the listView. Once you click the button, you will get the index of the row, and start to download the corresponding file.

      1. I tried but It return me error.
        here is my button buttonDownload in my Adapter.class:

        viewHolder.btnDetails = (Button) row.findViewById(R.id.btnDetails);
        viewHolder.btnDetails.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        View parent = (View)v.getParent();
        ListView listView = (ListView) parent.getParent();
        final int pos = listView.getPositionForView(parent);
        if(frameLayout.getVisibility()==View.GONE){
        showFrameLayout();
        } else {
        hideFrameLayout();
        }

        }
        });

        and out of create the following….

        public class ViewHolder{
        Button btnDownload;
        Button btnDetails;
        }

        private void showFrameLayout(){
        frameLayout = (FrameLayout) frameLayout.findViewById(R.id.frame_layout_listview);
        frameLayout.setVisibility(View.VISIBLE);
        }
        private void hideFrameLayout(){
        frameLayout = (FrameLayout) frameLayout.findViewById(R.id.frame_layout_listview);
        frameLayout.setVisibility(View.GONE);
        }
        but when I push the button it goes to open another cell into a listView. can you tell me where is my mistake?

        1. sorry I wrote a mistake. the Button is button=btnDetails
          and It should open a FrameLayout of each issue. but when I select button one it opes issue 3.
          I think there is something wrong in getPosition.

          1. this is from the Adapter.class
            @Override
            public View getView(final int pos, final View convertView, final ViewGroup parent){
            RelativeLayout row = (RelativeLayout)convertView;
            Log.i(“AtlantisSites”, “getView pos = ” + pos);
            //ViewHolder mainViewHolder = null;
            if(null == row){

            this is how I get position

  9. Hi,
    Your post is useful and i do the settag method.
    But i want to change the visibility (GONE) of the button that is clicked on a particular row and show another button in place of it.

    Can you tell me how it is possible ?

    Right now i tried multiple solutions but nothing worked for me as of now. Because when I clicked on a button in a particular row, buttons in other row also changed.

    Please reply

    1. As your description, it looks like that you are using some high performance solution such as recycling views or ViewHolder, which will reuse the views for visible row in the list. I suggest you need to remember the button state for each row, and then set the button state each time in getView function.

  10. You can’t use findViewById in getView() , where do you do that ?

    I need the second part in my app but I am getting a null pointer exception.

  11. sir i want to have a click on list items but for different columns should have different effect in single list items

Leave a Reply to Dhwani Cancel reply

Your email address will not be published. Required fields are marked *