Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

android - Practical Difference Between position and row id in onListItemClick()

I'm confused about the difference between postion and row id in ListActivity.onListItemClick().

The documentation has the following:

position  The position of the view in the list
id        The row id of the item that was clicked

Is there any practical difference between the two? I assume, since there are two different parameters, that there are situations where the values are different, but based on the wording used in the documentation, it's hard to see what that difference may be. Is it that one of them can hold the value of a database row ID provided by a Cursor or some other special capability?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

position: The position of the view in the list
id: The row id of the item that was clicked

A position starts from 0, top to bottom of the ListView, whereas to get a proper row id it is important that the cursor, which was passed to the adapter, contains a column called '_id' representing unique id for each row in the table.

If you are using an ArrayAdapter, position and id become the same. The id will be the returned value of Adapter.getItemId(int) so if you use an ArrayAdapter it will be the same as the position - a quick search of the ArrayAdapter source code shows it's using return position; to work out the id.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...