AdapterView<?>是什么?在OnitemClick()方法中意味着什么?其他参数有什么用呢? [英] What does AdapterView<?> mean in the OnitemClick() Method? What is the use of other parameters in it?

查看:175
本文介绍了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();
        }
    });


推荐答案

<?> ; 表示泛型。在此处了解更多信息。

The <?> indicates a Generic. Read more about them here.

这是文档说说参数:

onItemClick(AdapterView<?>父级,视图视图,整数位置,长ID)

onItemClick(AdapterView<?> parent, View view, int position, long id)


父级 AdapterView,点击

parent The AdapterView where the click happened.

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

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

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

position The position of the view in the adapter.

id 单击的项目的行ID。

id The row id of the item that was clicked.

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

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);

视图是指其中的特定项目 AdapterView 。在 ListView 中,该行。因此,您可以通过这样说: TextView 来获得对行的引用:

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();

位置视图的位置 父级中。对于 ListView ,它是行号。第一行是位置0,第二行是位置1,第三行是位置2,依此类推。请注意,如果您的 ListView 具有标题视图(例如 ListView.addHeaderView(View)),则标头视图的位置为0,实际行的编号从1开始。

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.

有时 id 位置相同,有时又有所不同。如果您使用的是 ArrayAdapter SimpleAdapter ,则它们是相同的(除非有标题视图,然后他们被一个掉了)。对于 CursorAdapter (因此是 SimpleCursorAdapter ), id 会返回表,即 _id

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:

  • https://stackoverflow.com/a/9863279/3681880
  • https://stackoverflow.com/a/12966006/3681880
  • https://stackoverflow.com/a/24531354/3681880

这篇关于AdapterView&lt;?&gt;是什么?在OnitemClick()方法中意味着什么?其他参数有什么用呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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