什么是方法的getItem和getItemId在Android类BaseAdapter的意图是什么? [英] What is the intent of the methods getItem and getItemId in the Android class BaseAdapter?

查看:188
本文介绍了什么是方法的getItem和getItemId在Android类BaseAdapter的意图是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇的方法的目的<一href="http://developer.android.com/reference/android/widget/Adapter.html#getItem%28int%29">getItem和<一href="http://developer.android.com/reference/android/widget/Adapter.html#getItemId%28int%29">getItemId在类适配器在Android SDK。

I'm curious about the purpose of the methods getItem and getItemId in the class Adapter in the Android SDK.

从描述,似乎的getItem应返回的基础数据。所以,如果我有名字[猫,狗,红色]数组,并创建一个适配器的的使用,那么的 a.getItem(1)应该返回狗,是否正确?应该采取什么的 a.getItemId(1)的回报?

From the description, it seems that getItem should return the underlying data. So, if I have an array of names ["cat","dog","red"] and I create an adapter a using that, then a.getItem(1) should return "dog", correct? What should a.getItemId(1) return?

如果你已经利用了在实践中这些方法,你能提供一个例子吗?

If you've made use of these methods in practice, could you provide an example?

推荐答案

我看到这些方法来访问我的列表中的数据更清洁的方式。相反,通过类似 myListData.get直接访问我的适配器对象(位置)我可以简单地调用像 adapter.get(位置)适配器

I see these methods as a cleaner approach to accessing my list's data. Instead of directly accessing my adapter object via something like myListData.get(position) i can simply call the adapter like adapter.get(position).

这同样适用于 getItemId 。通常,当我想执行的基础上,在列表中的对象的唯一标识某个任务我会用这种方法。使用数据库时,这是特别有用的。返回 ID 可能是一个参考数据库中的对象,然后我可以在(更新/删除/等)。

The same goes for getItemId. Usually I would use this method when I want to execute some task based on the unique ID of an object in the list. This is especially useful when working with a database. The returned id could be a reference to an object in the database which I then could perform different operations on(update/delete/etc).

因此​​,而不是访问从原始数据对象的ID如 myListData.get(位置).getId()您可以使用 adapter.getItemId (位置)

So instead of accessing the ID from the raw data object like myListData.get(position).getId() you can use adapter.getItemId(position).

在这里我已经觉得我需要使用这些方法的一个例子是使用的 SeparatedListViewAdapter 。该适配器可以包含多个不同类型的适配器,每个重不同类型(典型值)的presenting数据。当调用的getItem(位置) SeparatedListViewAdapter ,返回的对象可能不同,具体取决于哪个一节上的位置是你把它。

One example of where i've felt like I needed to use these methods was in a project using the SeparatedListViewAdapter. This adapter can contain multiple different kinds of adapters, each representing data of a different type(typically). When calling getItem(position) on the SeparatedListViewAdapter, the object returned may be different depending on which "section" the position is that you send it.

例如,如果你在你的列表中有2个部分(水果和糖果):如果你使用的getItem(位置)位置碰巧就在水果中的项目部分,您将获得不同的对象,比你要求的的getItem(位置)位置指向一个项目在糖果部分。你可能会再回到某种恒定的ID值 getItemId(位置)从而重新presents什么样的数据的getItem(位置)正在恢复,或使用的instanceof 来确定您是否有什么反对。

For example, if you had 2 sections in your list(fruit and candy): If you used getItem(position) and position happened to be on an item in the fruit section, you would receive a different object than if you requested getItem(position) with position pointing to an item in the candy section. You might then return some sort of constant ID value in getItemId(position) which represents what kind of data getItem(position) is returning, or use instanceof to determine what object you have.

除了我所提到的那样,我从来没有觉得我真的需要使用这些方法

Other than what I've mentioned, I've never felt like I really needed to use these methods

这篇关于什么是方法的getItem和getItemId在Android类BaseAdapter的意图是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆