15

When we implement OnItemClickListener, we have to implement onItemClick method which is an abstract method in OnItemClickListener interface. In onItemClick method there are four arguments.

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    Toast.makeText(this, "Clicked on : " + arg2 + " long arg : " + arg3, 
                   Toast.LENGTH_LONG).show();
}

What I need to know is the difference between last two arguments (int arg2, long arg3). As you can see in my code, when I try to Toast it I get the same value for both arguments.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
AnujAroshA
  • 4,623
  • 8
  • 56
  • 99
  • I have figure it out clearly... [link](http://anujarosha.wordpress.com/2011/11/30/how-to-use-baseadapter-and-onitemclicklistener-in-android/) – AnujAroshA Dec 21 '11 at 10:29

5 Answers5

35

I had the same question as you. However, the answers here were not extremely helpful. I don't support the go-look-it-up-yourself answers, especially when the so called documentation is so unclear. I did look it up myself, though, and the following is what I found.

The int value is the position of the view in the parent. For a ListView, it is the row number. The top row is position 0, the second row is position 1, the third row is position 2, etc. Note that if your ListView has a header view (like if you did ListView.addHeaderView(View)) then the header view would be position 0 and the actual rows would start their numbering at 1.

Sometimes the long value is the same as the int position and sometimes it is different. If you are using an ArrayAdapter or SimpleAdapter then they are the same (except in the case that there is a header view and then they are off by one). For a CursorAdapter (and consequently a SimpleCursorAdapter) the long id returns the row id of the table, that is, _id. It is a long rather than an int because a database could theoretically have more rows than an int could hold whereas a ListView wouldn't.

Here are a few other related answers:

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
3

Consider reading the documentation.

The int is the view position, the long is the item ID.

(We can't see that you get the same value for both; we only see your code, not your screen.)

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 2
    I can't upload any images caz I'm new to Stackoverflow and also I have read the documentation and they explain it in the same way. I need a more explanation. That's why I posted the question here. I'm very glad if you can point me to a code example where I can see how can we use those last two arguments. – AnujAroshA Nov 22 '11 at 05:54
2

The given answers are so helpful. But the needed answer, I mean the exact answer I wanted was posted as a comment for the question by myself. But that wont increase my "accept level". So I thought to put the link as the answer for the question. Here is the link to the answer.

AnujAroshA
  • 4,623
  • 8
  • 56
  • 99
1

In my opinion, through the position, you can get row item with adapter's getItem(position). So, the position mean the data position in the adapter.

For the id parameter, I think it is a help method, as you know, the data in adapter always is a recorder. general speaking, a recorder should have an id column(something like the database id). when coding, you can get the item through position, then get the item's id(if the item has id). but you can get such "id" directly with "id" parameter. you may noticed: the position is "int" type while "id" is "long" type.

By the way, if you want use the id parameter, you have to implement the getItemId() method in adapter. the default implement in ArrayAdapter is just return the position.

David Guo
  • 1,749
  • 3
  • 20
  • 30
1

int value represents position of item, and long value represents item Id..

public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)

parent      The AdapterView where the click happened.
view        The view within the AdapterView that was clicked
            (this will be a view provided by the adapter)
position    The position of the view in the adapter.
id          The row id of the item that was clicked.
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
  • In a List, when we are clicking on an item, that callback the "position" of that item. So what is the "id". If you can explain some thing like that I will be very thankful. I have done my home work (read the Android documentation) before putting this question here. So please explain some thing more than or something different than the doc. Thank You (http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html#onItemClick%28android.widget.AdapterView%3C?%3E,%20android.view.View,%20int,%20long%29) – AnujAroshA Nov 22 '11 at 06:05
  • let us take 5 items in the list, the positions start from zero i.e(0,1,2,3,4) are the positions for items and id means automatically it will be generated by android at run time for recognize the items. – RajaReddy PolamReddy Nov 22 '11 at 06:11
  • This is the out put I am getting http://twitpic.com/7hq4yw for the code public void onItemClick(AdapterView> parent, View arg1, int arg2, long arg3) { Toast.makeText(this, "Clicked on : " + arg2 + " long arg : " + arg3, Toast.LENGTH_LONG).show(); } @Polam can you explain why? Thank You – AnujAroshA Nov 22 '11 at 06:46
  • Clicked on : 1 means u clicked on item 2 from top because of items starting from 0 from top. long arg : 1 that is the item Id value. why u required this much explanation about id we are not using Id's here, we require pos that is sufficient for using items. – RajaReddy PolamReddy Nov 22 '11 at 06:49
  • @PolamReddyRajaReddy Yep, I know that Arrays are 0 based. But what I can't understand is why am I getting same value for the arg2 and arg3. Can you tell me an example where I can get different values for those arguments. Thanks in advance. – AnujAroshA Nov 22 '11 at 06:53