I'm attempting to create an onClick listener for each item in my imageCarousel - however I'm unable to create a unique listener for each object in the carousel.
Currently - the listener functions - but it applies to all items in the carousel - and my attempts at finding/using the unique ID for each item has failed.
Current implementation:
for (int i = 0; i < titleResourcesTypedArray.length(); ++i) {
// Create new ImageView
imageItem = new ImageView(this);
// Set the shadow background
imageItem.setBackgroundResource(R.drawable.shadow);
// Set the image view resource
imageItem.setImageResource(titleResourcesTypedArray.getResourceId(
i, -1));
// Set the size of the image view to the previously computed value
imageItem.setLayoutParams(new LinearLayout.LayoutParams(imageWidth,
imageWidth));
//imageItem.setOnClickListener(this);
// Add image to the carousel
mCarouselContainer1.addView(imageItem);
imageItem.setOnClickListener(new View.OnClickListener() {
// create an onClick Listener
@Override
public void onClick(View v) {
// create an onClick Event (Start the Map Download)
Intent intent = new Intent(Home.this, Download.class);
startActivity(intent);
}
});
}
Desired implementation:
I believe I should be using something along the lines of:
imageItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
titleResourcesTypedArray.getResources();
if (v.getId() = 7) {
Intent intent = new Intent(Home.this, Download.class);
startActivity(intent);
...
or perhaps:
imageItem.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
titleResourcesTypedArray.getResources();
if (v.getResources() = 10) {
// Do Stuff
Intent intent = new Intent(Home.this, Download.class);
startActivity(intent);
}
}
}
How might I properly identify the ID of each item in a carousel?
In addition to comparing with ids, there is an additional way to use a 'tag'. You can set a tag on the imageView using the setTag method and then in the onClick method you can compare this tag.