There is a MainActivity in which I list the stations into a fragment via TabLayout . And the fragment in turn is used for the tab.

Fragment code:

 public class TabFragment1 extends Fragment implements ItemSendId { private ArrayList<Radio> radioList = new ArrayList<>(); private RecyclerView radioListRecycler; RadiosAdapter mRadioAdapter; DBHeler db; @Override public void onStart() { super.onStart(); db = new DBHeler(getContext()); radioListRecycler = (RecyclerView) getActivity().findViewById(R.id.radioListRecycler); mRadioAdapter = new RadiosAdapter(radioList); mRadioAdapter.setItemSendId(this); radioListRecycler.setLayoutManager(new GridLayoutManager(getContext(), 3)); radioListRecycler.setItemAnimator(new DefaultItemAnimator()); radioListRecycler.setAdapter(mRadioAdapter); prepareRadioData(); } @Override public void sendItemId(int id) { Intent intent = new Intent(getContext(), PlayerActivity.class); intent.putExtra("id", id); startActivity(intent); } private void search(SearchView searchView) { searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { if (mRadioAdapter != null) mRadioAdapter.getFilter().filter(newText); return true; } }); } private void prepareRadioData() { //заполняем коллекцию } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.tab_fragment_1, container, false); } } 

In MainActivity before I redid the application using TabLayout , the search worked. Now I have a problem. In menu.xml posted component SearchView .

Menu code from MainActivity :

 @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); MenuItem search = menu.findItem(R.id.search); SearchView searchView = (SearchView) MenuItemCompat.getActionView(search); search(searchView); return true; } 

How to access the MainActivity variable from the mRadioAdapter , which is also used in the search method of the TabFragment1 fragment.

If it is not clearly described, then this is what I now have and what I need to get:

There are tabbed activations. The first tab displays the radio station. All code for outputting stations is implemented in TabFragment1.java . Now you need to click on the search in the MainActivity menu to search for stations. Everything worked before all the fragment code was in the MainActivity , but after transferring it to the fragment I don’t know how to access the adapter from the activation.

  • You implement the onCreateOptionsMenu method in the necessary fragment, and in the same place implement the search - McDaggen
  • tried it. Did not help - Coconut
  • From your words it follows that you transferred all the logic from the activation to the fragment, except for the onCreateOptionsMenu method, and now the task is to get from this method which is in the activation list which is in the fragment. If you correctly implement the remaining onCreateOptionsMenu onCreateOptionsMenu in the fragment, then in theory you should have no problems. - McDaggen
  • And how to implement? I am writing this does not work. @Override public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {super.onCreateOptionsMenu (menu, inflater); inflater.inflate (R.menu.main, menu); MenuItem search = menu.findItem (R.id.search); SearchView searchView = (SearchView) MenuItemCompat.getActionView (search); search (searchView); } - Coconut
  • If you remove the onCreateOptionsMenu method from the MainActivity, then when you start it, the menu disappears altogether and the SearchView icon disappears - Coconut

0