Android AdapterView<?> 在 OnitemClick() 方法中是什么意思?里面的其他参数有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3184672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does AdapterView<?> mean in the OnitemClick() Method? What is the use of other parameters in it?
提问by Nandagopal T
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
采纳答案by Matty F
The <?>
indicates a Generic. Read more about them here.
该<?>
指示通用。在此处阅读有关它们的更多信息。
Here is what the documentationsays about the parameters:
以下是文档中关于参数的说明:
onItemClick(AdapterView<?> parent, View view, int position, long id)
onItemClick(AdapterView<?> parent, View view, int position, long id)
parentThe AdapterView where the click happened.
viewThe view within the AdapterView that was clicked (this will be a view provided by the adapter)
positionThe position of the view in the adapter.
idThe row id of the item that was clicked.
parent发生点击的 AdapterView。
view被点击的 AdapterView 中的视图(这将是适配器提供的视图)
position视图在适配器中的位置。
id单击的项目的行 id。
The AdapterView
could be a ListView
, GridView
, Spinner
, etc. The question mark inside the angle brackets indicates that it could be any of them. This is called genericsin Java. You can use parentin code to do something to the whole view. For example, if you were using a ListView
you could hide the whole ListView
by the following line of code:
在AdapterView
可能是ListView
,GridView
,Spinner
等尖括号内的问号表明它可以是任何人。这在 Java 中称为泛型。您可以在代码中使用parent对整个视图执行某些操作。例如,如果您使用的是 aListView
您可以ListView
通过以下代码行隐藏整个:
parent.setVisibility(View.GONE);
The View
refers to a specific item within the AdapterView
. In a ListView
it is the row. Thus, you can get a reference to a TextView
within a row by saying something like this:
在View
指内的特定项目AdapterView
。在 aListView
它是行。因此,您可以TextView
通过这样说来获得对一行中的a 的引用:
TextView myTextView = (TextView) view.findViewById(R.id.textView1);
String text = myTextView.getText().toString();
The positionis the position of the viewin 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.
所述位置是所述的位置图中的亲本。对于 a ListView
,它是行号。顶行是位置 0,第二行是位置 1,第三行是位置 2,等等。请注意,如果你ListView
有一个标题视图(就像你那样ListView.addHeaderView(View)
),那么标题视图将是位置 0,实际行会从 1 开始编号。
Sometimes idis the same as positionand 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 idreturns the row id of the table, that is, _id
.
有时id与位置相同,有时则不同。如果您使用的是ArrayAdapter
orSimpleAdapter
那么它们是相同的(除非有标题视图然后它们相差一个)。对于 a CursorAdapter
(以及 a SimpleCursorAdapter
),id返回表的行 id,即_id
。
Here are a few other good answers on this topic:
以下是有关此主题的其他一些很好的答案:
回答by Matty F
The ? means that the datatype is unknown and it can be any type.
这 ?意味着数据类型是未知的,它可以是任何类型。