Android: Listview 给每个item指定的id

Android: Listview give each item specific id

作为一名开发人员,我是一个新手,所以如果这个问题看起来有点菜鸟,我很抱歉。

所以我有这个 ListView 和自定义适配器,其中每一行都有自己的图像。这是每行设置图片的摘录:

  if (position == 0){


            ImgView1.setImageResource(R.drawable.Image1);


    //Here I want to assign specific id to the imageview at this position***
    //Like this? :    ImageView1.setTag(position); 
}

此外,我想在每个位置的 OnItemClickListener 中找到另一个 class,即带有列表视图的那个,以找到该行中使用的图像视图的特定 ID。以下是摘录:

if (position == 0) {

                //Something here to get the position of the Image in this row or something, so i can use only that in the ActivityOptionsCompat


                ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(getApplicationContext(),**THE IMAGE FOR THIS ROW**,"champImage2")
                Intent intent = new Intent (this, ActivityB.class);
                startActivity(intent,optionsCompat.toBundle()); 

            }

感谢任何帮助:) (抱歉我的英语不好,这不是我的母语)

您应该在用于建模数据的 class 中包含图像 ID。然后在 getView() 中,您应该检索模型并使用如下方式将其绑定到您的视图:

public class Model {
    @DrawableRes private int imageId;
}

...

Model myModel = adapter.getItem(position);
myModel.imageId;
// TODO display image & pass to activity