Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "int" and "long" argument in onItemClick in Android

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.

like image 811
AnujAroshA Avatar asked Nov 22 '11 05:11

AnujAroshA


4 Answers

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:

  • https://stackoverflow.com/a/25622142/3681880
  • https://stackoverflow.com/a/9863279/3681880
  • https://stackoverflow.com/a/12966006/3681880
  • https://stackoverflow.com/a/24531354/3681880
like image 96
Suragch Avatar answered Nov 16 '22 01:11

Suragch


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.)

like image 37
Dave Newton Avatar answered Nov 16 '22 02:11

Dave Newton


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.

like image 41
AnujAroshA Avatar answered Nov 16 '22 01:11

AnujAroshA


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.
like image 30
RajaReddy PolamReddy Avatar answered Nov 16 '22 02:11

RajaReddy PolamReddy