AdapterView> 的作用是什么? OnitemClick() 方法中的意思是什么?那里面其他参数有什么用呢?
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
表示通用。在此处了解有关它们的更多信息。
以下是文档有关参数的说明:
onItemClick(AdapterViewparent, View view, int position, long id)
AdapterView
可以是ListView
、GridView
、Spinner
等。尖括号内的问号表示它可以是其中任何一个。这在 Java 中称为泛型。您可以在代码中使用 parent 对整个视图执行某些操作。例如,如果您使用的是ListView
,则可以通过以下代码行隐藏整个ListView
:View
引用特定项目在AdapterView
中。在ListView
中,它是行。因此,您可以通过如下方式获取对行中TextView
的引用:position 是行中 view 的位置。 父母。对于
ListView
,它是行号。最上面一行是位置 0,第二行是位置 1,第三行是位置 2,等等。请注意,如果您的ListView
有标题视图(就像您执行了ListView.addHeaderView (View)
) 那么标题视图将是位置 0,实际行将从 1 开始编号。有时 id 与 position 相同,有时这是不同的。如果您使用的是
ArrayAdapter
或SimpleAdapter
,那么它们是相同的(除非存在标题视图,然后它们相差一)。对于CursorAdapter
(以及SimpleCursorAdapter
),id 返回表的行 ID,即_id
。以下是有关此主题的其他一些很好的答案:
The
<?>
indicates a Generic. Read more about them here.Here is what the documentation says about the parameters:
onItemClick(AdapterView<?> parent, View view, int position, long id)
The
AdapterView
could be aListView
,GridView
,Spinner
, etc. The question mark inside the angle brackets indicates that it could be any of them. This is called generics in Java. You can use parent in code to do something to the whole view. For example, if you were using aListView
you could hide the wholeListView
by the following line of code:The
View
refers to a specific item within theAdapterView
. In aListView
it is the row. Thus, you can get a reference to aTextView
within a row by saying something like this:The position 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 yourListView
has a header view (like if you didListView.addHeaderView(View)
) then the header view would be position 0 and the actual rows would start their numbering at 1.Sometimes id is the same as position and sometimes it is different. If you are using an
ArrayAdapter
orSimpleAdapter
then they are the same (except in the case that there is a header view and then they are off by one). For aCursorAdapter
(and consequently aSimpleCursorAdapter
) the id returns the row id of the table, that is,_id
.Here are a few other good answers on this topic:
这 ?表示数据类型未知,可以是任何类型。
The ? means that the datatype is unknown and it can be any type.