AdapterView 的作用是什么? OnitemClick() 方法中的意思是什么?那里面其他参数有什么用呢?

发布于 2024-09-08 15:32:07 字数 286 浏览 2 评论 0 原文

gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扮仙女 2024-09-15 15:32:07

表示通用。在此处了解有关它们的更多信息。

以下是文档有关参数的说明:

onItemClick(AdapterViewparent, View view, int position, long id)

parent发生点击的 AdapterView。

view AdapterView 中被单击的视图(这将是适配器提供的视图)

位置视图在适配器中的位置。

id 被单击的项目的行 ID。

AdapterView 可以是 ListViewGridViewSpinner 等。尖括号内的问号表示它可以是其中任何一个。这在 Java 中称为泛型。您可以在代码中使用 parent 对整个视图执行某些操作。例如,如果您使用的是 ListView,则可以通过以下代码行隐藏整个 ListView

parent.setVisibility(View.GONE);

View 引用特定项目在 AdapterView 中。在 ListView 中,它是行。因此,您可以通过如下方式获取对行中 TextView 的引用:

TextView myTextView = (TextView) view.findViewById(R.id.textView1);
String text = myTextView.getText().toString();

position 是行中 view 的位置。 父母。对于ListView,它是行号。最上面一行是位置 0,第二行是位置 1,第三行是位置 2,等等。请注意,如果您的 ListView 有标题视图(就像您执行了 ListView.addHeaderView (View)) 那么标题视图将是位置 0,实际行将从 1 开始编号。

有时 idposition 相同,有时这是不同的。如果您使用的是ArrayAdapterSimpleAdapter,那么它们是相同的(除非存在标题视图,然后它们相差一)。对于 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)

parent The AdapterView where the click happened.

view The view within the AdapterView that was clicked (this will be a view provided by the adapter)

position The position of the view in the adapter.

id The row id of the item that was clicked.

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 generics in Java. You can use parent in 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:

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:

TextView myTextView = (TextView) view.findViewById(R.id.textView1);
String text = myTextView.getText().toString();

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 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.

Sometimes id is the same as position and 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 id returns the row id of the table, that is, _id.

Here are a few other good answers on this topic:

将军与妓 2024-09-15 15:32:07

这 ?表示数据类型未知,可以是任何类型。

The ? means that the datatype is unknown and it can be any type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文