I have a ListView. Naturally I pass an adapter to it, say mDogAdapter. My ListView also implements setOnItemClickListener.

So here is my question: in the context of getting an item from the adapter, what is the difference between these two?

Dog dog =(Dog) parent.getItemAtPosition(position);
Dog dog = mDogAdapter.getItem(position);
有帮助吗?

解决方案

There's no difference.

The AdapterView's getItemAtPosition() method will call getItem() on the adapter in its implementation.

It's up to you to decide which makes more sense. If it's possible that you may use a different adapter at some point, you should consider using the AdapterView's getItemAtPosition() method -- let the AdapterView deal with getting the correct data from its Adapter.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top