1

I have implemented a ViewPager using PagerAdapter, now in the PagerAdpater I have the following which works fine:

@Override
public Object instantiateItem(ViewGroup container, final int position) {
ImageView imgDisplay, ivRateBar;
TextView tvName, tvPhoneNumber, tvRate;

inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.viewpager_item, container,false);

tvName = (TextView) viewLayout.findViewById(R.id.tvName);
tvName.setText(_textNames.get(position));

....

Now I have a button in the PagerAdapter, I can get the position of item clicked (in this case the name) by calling:

_textNames.get(position)

Now this works well when the buttons are within the PagerAdapter but I want my buttons to be within the MainActivity instead so that the view pager doesn't scroll the buttons (they should be fixed) now I am not sure how to get the position of the item clicked from MainActivity, I tried:

Toast.makeText(getApplicationContext(), "clicked: "+ViewPagerAdapter._textNames.get(position), Toast.LENGTH_SHORT).show();

but this doesn't work because my MainActivity does not recognize position from .get(position).

user3560827
  • 632
  • 1
  • 5
  • 22
  • Your question isn't very clear. If the `Button`s aren't in the `ViewPager`, how do they have a position? Are you basically just wanting the current position of the `ViewPager`? – Mike M. Nov 19 '16 at 06:16
  • I want to return the name of the person in the viewpager, when I scroll and click button again, the name returned should be different (matching the current viewpager) – user3560827 Nov 19 '16 at 06:23
  • That post shows how to get the current position. Use that with your `_textNames` collection. – Mike M. Nov 19 '16 at 06:26
  • I got it: Toast.makeText(getApplicationContext(), "clicked: "+names.get(viewPager.getCurrentItem()), Toast.LENGTH_SHORT).show(); – user3560827 Nov 19 '16 at 08:47

0 Answers0