Reputation: 5213
I want to get the position of Image View on Image Click listener which is added inside horizontal Scroll View dynamically using for loop. And that position want to set the View pager current item in another Activity.
I'm trying this method:
int numpos = imageView.getId();
Log.e("numpos "," = "+ numpos);
Every view position is getting -1.
Here is my code:
private void prepareTextViewsForHistory(){
HorizontalScrollView parent=(HorizontalScrollView)findViewById(R.id.horizontal_scroll_view_id_01);
LinearLayout layout=new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
parent.addView(layout);
for (int i=DEFAULT_HISTORY_MAX - 1; i >= 0; i--)
{
final ImageView imageView=new ImageView (this);
imageView.setTag(i);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Log.e("imageView ", " Click ");
int numpos = imageView.getId();
Log.e("numpos "," = "+ numpos);
String item_ActivityId = all_Post.getStrActivityId();
Toast.makeText(getBaseContext(), String.valueOf(v.getId()), Toast.LENGTH_SHORT).show();
Log.e(" Image ID = "," = "+String.valueOf(v.getId()));
Intent intent = new Intent(getContext(),SimpleImageActivity.class);
//intent.putExtras(extras);
intent.putExtra("activity_id",item_ActivityId);
startActivity(intent);
}
});
layout.addView(textView);
}
}
Upvotes: 1
Views: 1162
Reputation: 2208
You are using imageView.setTag(i);
for setting tag, use v.getTag().toString()
inside onClick(View v )
method.
Upvotes: 0
Reputation: 8853
you can do this by setting a Tag while creating a view
in your loop where you have created your view just add this line :
yourView.setTag("your incremental variable");
yourView.setOnClickListener(this);
and in onclick(View v)
you can get that position by
v.getTag().toString()
Upvotes: 3