简体   繁体   中英

Android: Listview give each item specific id

I am quite new as an developer so I'm sorry if this question seems a bit noobish.

So I have this ListView with a custom adapter, where each row has its own image. Here is a excerpt from where the picture is set for each row:

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

Further, I want to i an other class, the one with the listview, in the OnItemClickListener for each position to find the specific id for the imageview used in that row. Here is an excerpt:

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

            }

Appreciate any help :) (Sorry for my bad english, it is not my first language)

You should include the image id in the class you are using to model your data. Then within getView(), you should retrieve the model and bind it to your view using something like this:

public class Model {
    @DrawableRes private int imageId;
}

...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM