In the application resykler is in the fragment. You need to get the index of the item you clicked to display certain information.

Wrote a callback, it passes from the adapter to the index fragment. But I ran into a stupid task, to which I don’t really know how to approach.

The fact is that this fragment is in tabs, and it is added as follows:

String tabs[] = { "FORM", "PREVIEW" }; public SuggestTabsPagerFragmentAdapter(FragmentManager fm) { super(fm); } @Override public CharSequence getPageTitle(int position) { return tabs[position]; } @Override public Fragment getItem(int position) { switch (position) { case 0: return SuggestTermFragment.getInstance(); case 1: return SuggestTermFragment.getInstance(); } return null; } @Override public int getCount() { return tabs.length; } } 

In other words, FragmentManager not used, and I don’t really imagine how to get the necessary index for activation from a fragment.

Tried to make a method (not static), which gets intu. But through getActivity() this method was not visible (obviously led to MainActivity - did not help).

Principle, you can do static, but it is too. Maybe there is some more humane way out?

UPD:

enter image description here

  • Reduction to MainActivity should work. Show how it didn't work for you - YuriySPb
  • @YuriySPb updated the answer - Silento
  • @YuriSPb public int setSheetIndex(int sheetIndex){ return sheetIndex; } public int setSheetIndex(int sheetIndex){ return sheetIndex; } Activation Method - Silento

1 answer 1

You incorrectly syntactically cast the result of a method call to a class. You need so, bracing the result:

 ((MainActivity)getActivity()).METHOD_FROM_MAIN_ACTIVITY_CLASS(); 

Or so - by creating a variable of the desired type:

 MainActivity activityMain = (MainActivity) getActivity(); activityMain.METHOD_FROM_MAIN_ACTIVITY_CLASS(); 
  • Well, the hand is simple = _ = - Silento