android 通过setOnItemClickListener获取与该列表项关联的数据

iugsix8n  于 2023-02-20  发布在  Android
关注(0)|答案(1)|浏览(104)

我已经用这种方式通过适配器添加了一些列表项。

classStudentsCollection.add(new mClassStudents(R.drawable.ic_profile, "DataOne", "DataTwo", "DataThree"));

我有一个侦听器,用于侦听任何列表项上的单击事件。

classStudentsView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(mainAppContext, "position" + position + "id "+id, Toast.LENGTH_SHORT).show();
                //get data associated with this particular list object
            }
        });

现在我想检索与这个mClassStudents列表对象关联的所有数据,也就是检索值"DataOne", "DataTwo", "DataThree"
我该怎么做呢?

toiithl6

toiithl61#

若要检索单击的对象,只需调用

parent.getItemAtPosition(position);

并将返回值强制转换为特定对象。如果创建了自定义适配器,则为了正常工作,getItem必须返回与强制转换的项相同的项

相关问题