3

For my academic project, I want create an application with 4 tabs. The first one will show recent games added to a list, the second one will be a search form, the third will show the search result, and the last one will show the details. I currently have created the code for TabView and the 4 tabs. The problem is that I want to perform a search to get the items I have in a list which meet the search criteria on fragment 2, but I don't know how to pass the data from fragment 2 (textView data and spinner) to fragment 3. My code is the following:

MainActivity.java:

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.view.MenuInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setImageResource(R.drawable.ic_search_white_24dp);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
            }
        });

        //TabLayout function call
        configureTabLayout();

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case R.id.menu_exit:
                finish();
                return true;
            case R.id.menu_settings:
                Toast.makeText(this, "Under Construction", Toast.LENGTH_LONG).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }




    //Tab Layout function declaration
    private void configureTabLayout() {

        //Getting the tab layout
        TabLayout tabLayout = findViewById(R.id.tab_layout);

        //Adding Tabs
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_home_white_24dp));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_search_white_24dp));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_results_white_24dp));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_details_white_24dp));


        //The TabPagerAdapter instance is then
        //assigned as the adapter for the ViewPager and the TabLayout component added
        //to the page change listener
        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new TabPagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);


        //Finally, the onTabSelectedListener is configured on the TabLayout instance and
        //the onTabSelected() method implemented to set the current page on the
        //ViewPager based on the currently selected tab number.
        viewPager.addOnPageChangeListener(new     TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }
}

TabPagerAdapter.java

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabPagerAdapter extends FragmentPagerAdapter{

    int tabCount;

    public TabPagerAdapter(FragmentManager fm, int numberOfTabs) {
        super(fm);
        this.tabCount = numberOfTabs;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new HomeScreenFragment();
            case 1:
                return new SearchFormFragment();
            case 2:
                return new SearchResultsFragment();
            case 3:
                return new DetailsScreenFragment();
            default:
                return null;
        }
    }
    @Override
    public int getCount() {
        return tabCount;
    }
}

SearchFormFragment.java

package gr.pliroforiki_edu.videogamedb;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


/**
 * A simple {@link Fragment} subclass.
 */
public class SearchFormFragment extends Fragment {

    private Button searchButton;
    private EditText gameTitleEditText;

    Spinner spinnerGenre;



    public SearchFormFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        View searchFormView = inflater.inflate(R.layout.fragment_search_form, container, false);

        searchButton = searchFormView.findViewById(R.id.searchButton);
        gameTitleEditText = searchFormView.findViewById(R.id.game_title_editText);
        spinnerGenre = searchFormView.findViewById(R.id.genre_spinner);

        ArrayAdapter<CharSequence> genreAdapter = ArrayAdapter.createFromResource(
            getActivity(),
            R.array.game_genres,
            android.R.layout.simple_spinner_item
        );

        genreAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerGenre.setAdapter(genreAdapter);


        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String filterGameTitle = gameTitleEditText.getText().toString();
                int filterGenreId = spinnerGenre.getSelectedItemPosition();

                String message = String.format("Game Title: %s\n Genre: %s", filterGameTitle, filterGenreId);
                Toast.makeText(getActivity(),message, Toast.LENGTH_LONG).show();

            }
        });

        // Inflate the layout for this fragment
        //return inflater.inflate(R.layout.fragment_search_form, container, false);
        return searchFormView;

    }

}

SearchResultsFragment.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 */
public class SearchResultsFragment extends Fragment {

    TextView infoTextView;
    ListView listViewGames;




    public SearchResultsFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        View searchResultsView = inflater.inflate(R.layout.fragment_search_results, container, false);



        return searchResultsView;
    }

    private void findViews()
    {
        infoTextView = getActivity().findViewById(R.id.info_textView);
        listViewGames = getActivity().findViewById(R.id.games_listView);
    }


}

I want to archive the following via the fragments:

ListActivity.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;


public class ListActivity extends AppCompatActivity {

private TextView textViewInfo;
private ListView listViewBooks;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);

    // Animation when this Activity appears
    overridePendingTransition(R.anim.pull_in_from_right, R.anim.hold);

    // Get user filters from Intent
    Intent intent = getIntent();

    String filterAuthor = intent.getStringExtra("AUTHOR");
    String filterTitle = intent.getStringExtra("TITLE");
    int filterGenreId = intent.getIntExtra("GENREID", 0);

    findViews();

    // Show user filters for information
    String message = String.format("Author: %s\nTitle: %s\nGenreId: %d",
            filterAuthor, filterTitle, filterGenreId);
    textViewInfo.setText(message);

    DataStore.LoadBooks(filterAuthor, filterTitle, filterGenreId);

    //Complex Object Binding
    ListAdapter booksAdapter = new SimpleAdapter(
            this,
            DataStore.Books,
            R.layout.list_item,
            new String[]{DataStore.KEY_TITLE, DataStore.KEY_AUTHOR, DataStore.KEY_GENRENAME},
            new int[]{R.id.book_item_title, R.id.book_item_author, R.id.book_item_genre}
    );

    listViewBooks.setAdapter(booksAdapter);

    listViewBooks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent detailsIntent = new Intent(ListActivity.this, DetailsActivity.class);
            detailsIntent.putExtra(DataStore.KEY_POSITION, position);
            startActivity(detailsIntent);
        }
    });
}

@Override
protected void onPause(){
    overridePendingTransition(R.anim.hold, R.anim.push_out_to_right);
    super.onPause();
}

private void findViews(){
    textViewInfo = findViewById(R.id.textViewInfo);
    listViewBooks = findViewById(R.id.listViewBooks);
}


}

Mainactivity.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;



public class MainActivity extends AppCompatActivity {

private EditText textAuthor;
private EditText textTitle;
private EditText textGenre;
private Button buttonSearch;
private Spinner spinnerGenre;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DataStore.Init(getApplicationContext());

    textAuthor = findViewById(R.id.editTextAuthor);
    textTitle= findViewById(R.id.editTextAuthor);
    buttonSearch = findViewById(R.id.buttonSearch);
    spinnerGenre = (Spinner) findViewById(R.id.spinnerGenre);

    ArrayAdapter<CharSequence> genreAdapter = ArrayAdapter.createFromResource(
            this,
            R.array.book_genres,
            android.R.layout.simple_spinner_item
    );

    genreAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerGenre.setAdapter(genreAdapter);

    buttonSearch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String filterAuthor = textAuthor.getText().toString();
            String filterTitle = textTitle.getText().toString();
            int filterGenreId = spinnerGenre.getSelectedItemPosition();

            Intent intent = new Intent(MainActivity.this, ListActivity.class);
            intent.putExtra("AUTHOR", filterAuthor);
            intent.putExtra("TITLE", filterTitle);
            intent.putExtra("GENREID", filterGenreId);
            startActivity(intent);
        }
    });
}
}
ankuranurag2
  • 2,300
  • 15
  • 30

3 Answers3

1

Try this

pass value between two fragment using bundle

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabPagerAdapter extends FragmentPagerAdapter{

int tabCount;

 public TabPagerAdapter(FragmentManager fm, int numberOfTabs) {
super(fm);
this.tabCount = numberOfTabs;
}

 @Override
public Fragment getItem(int position) {
switch (position) {
    case 0:
        return  new HomeScreenFragment();
    case 1:
        return new SearchFormFragment();
    case 2:
       Fragment fragment = new SearchResultsFragment()
     Bundle args = new Bundle();
     args.putString("Key", "Value");
     fragment.setArguments(args);
        return fragment;

    case 3:
        return new DetailsScreenFragment();
    default:
        return null;
   }
}

    @Override
     public int getCount() {

   return tabCount;

   }
   }

in your onCreateView(....) of SearchResultsFragment

 String value = getArguments().getString("Key");

 public class SearchResultsFragment extends Fragment {

TextView infoTextView;
ListView listViewGames;

  public SearchResultsFragment() {
   // Required empty public constructor
 }


  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {

   View searchResultsView = 
  inflater.inflate(R.layout.fragment_search_results, container, false);
    String value = getArguments().getString("Key");


   return searchResultsView;
 }
 private void findViews(){

 infoTextView = getActivity().findViewById(R.id.info_textView);
listViewGames = getActivity().findViewById(R.id.games_listView);
 }
 }

hop its help you

Praveen
  • 946
  • 6
  • 14
0

You can use interface to send your search string to your activity from fragment 2, and from their you can call all methods in your fragment 3 as fragment3 object will be available to you in your activity, you can make performSearch() in your fragment3 and call it from your activity. Alternatively you can use something like event bus to avoid the boiler plate code needed to setup interface.

Have a look at this event bus repo https://github.com/greenrobot/EventBus Register event bus where you want the search string, in your case register the event bus in Fragment3 like this

  @Override
 public void onStart() {
 super.onStart();
 EventBus.getDefault().register(this);
 }

 @Override
 public void onStop() {
 super.onStop();
 EventBus.getDefault().unregister(this);
 }

In your Fragment 3 create a function like this

    @Subscribe
public void onSearchEvent(String searchString){
 //you will get your search string here
}

Now comeback to fragment2 from where you want to send the searchString, you have to put below code from where you want to send searchString, and this posted searchString will be received by fragment 3 in its onSearchEvent method

EventBus.getDefault().post(searchString);
Roshaan Farrukh
  • 399
  • 3
  • 10
  • Can you please provide some code example? I am really stuck now. I don't seem to find a way to get the search work. I my goal to perform search on json data and show results in a listview or recycleview – – Tasos Karatzoglou Feb 12 '19 at 21:34
  • If I have multiple search conditions; – Tasos Karatzoglou Feb 13 '19 at 09:28
  • What type of conditions? Please elaborate. – Roshaan Farrukh Feb 13 '19 at 09:30
  • 1-3 text field and 1-3 spinners. I just get the fields in variables and sent them via the bus function I create right; – Tasos Karatzoglou Feb 13 '19 at 09:47
  • Yes you can also send complete objects, e.g: You want to send the whole object name myObject of class MyObject from fragment3, you can do this by calling EventBus.getDefault().post(searchString) but now to receive this object in fragment2 you have to change the paramter type of searchEvent function like this @Subscribe public void searchEvent(MyObject object){} – Roshaan Farrukh Feb 13 '19 at 09:50
  • how do i call the object in my destination fragment?? onCreateView? – Tasos Karatzoglou Feb 13 '19 at 10:32
  • When you post the event from source fragment your destination fragment will receive this event inside method which is annoted by @Subscribe and have the same parameter type as of your posted item, so if you are posting string from source then in destination you will get it inside this function [at_the_rate]Subscribe public void anyname(String message){ //you can handle message here} , same is the case with object. you have to create a method in destination [at_the_rate]Subscribe void anyName(Object object){//here you can handle}. – Roshaan Farrukh Feb 13 '19 at 10:49
  • i understand that and i have created the EventBus.getDefault().post( new MessageEvent(searchString)); but how do i use this on the onCreateView? or i really missing something here. still i thank you for your time. – Tasos Karatzoglou Feb 13 '19 at 10:55
  • You won't use this in onCreateView your destination fragment should be already started before receving any events, if you want to open your fragment with data you can do it simply by using arguments. – Roshaan Farrukh Feb 13 '19 at 10:58
  • any guide how to do that? – Tasos Karatzoglou Feb 13 '19 at 11:01
0

There are many ways you can pass objects/values between fragments. In your case, the simplest solution would be to delegate those values to the holding Activity i.e MainActivity.

MainActivity:

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.view.MenuInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
//these will hold your values
String filterGameTitle;
int filterGenreId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setImageResource(R.drawable.ic_search_white_24dp);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    //TabLayout function call
    configureTabLayout();

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()){
        case R.id.menu_exit:
            finish();
            return true;
        case R.id.menu_settings:
            Toast.makeText(this, "Under Construction", Toast.LENGTH_LONG).show();
            return true;
            default:
                return super.onOptionsItemSelected(item);
    }
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}




//Tab Layout function declaration
private void configureTabLayout() {

    //Getting the tab layout
    TabLayout tabLayout = findViewById(R.id.tab_layout);

    //Adding Tabs
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_home_white_24dp));
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_search_white_24dp));
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_results_white_24dp));
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_details_white_24dp));


    //The TabPagerAdapter instance is then
    //assigned as the adapter for the ViewPager and the TabLayout component added
    //to the page change listener
    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new TabPagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);


    //Finally, the onTabSelectedListener is configured on the TabLayout instance and
    //the onTabSelected() method implemented to set the current page on the
    //ViewPager based on the currently selected tab number.
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }
        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }
        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });
}
}

SearchFormFragment:

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


/**
 * A simple {@link Fragment} subclass.
 */
public class SearchFormFragment extends Fragment {

private Button searchButton;
private EditText gameTitleEditText;

Spinner spinnerGenre;



public SearchFormFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View searchFormView = inflater.inflate(R.layout.fragment_search_form, container, false);

    searchButton = searchFormView.findViewById(R.id.searchButton);
    gameTitleEditText = searchFormView.findViewById(R.id.game_title_editText);
    spinnerGenre = searchFormView.findViewById(R.id.genre_spinner);

    ArrayAdapter<CharSequence> genreAdapter = ArrayAdapter.createFromResource(
            getActivity(),
            R.array.game_genres,
            android.R.layout.simple_spinner_item
    );

    genreAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerGenre.setAdapter(genreAdapter);


    searchButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String filterGameTitle = gameTitleEditText.getText().toString();
            int filterGenreId = spinnerGenre.getSelectedItemPosition();
            ((MainActivity)getActivity()).filterGameTitle = filterGameTitle;
            ((MainActivity)getActivity()).filterGenreId = filterGenreId;
            String message = String.format("Game Title: %s\n Genre: %s", filterGameTitle, filterGenreId);
            Toast.makeText(getActivity(),message, Toast.LENGTH_LONG).show();

        }
    });

    // Inflate the layout for this fragment
    //return inflater.inflate(R.layout.fragment_search_form, container, false);
    return searchFormView;

}

}

SearchResultFragment:

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 */
public class SearchResultsFragment extends Fragment {

TextView infoTextView;
ListView listViewGames;

String filterGameTitle;
int filterGenreId;

public SearchResultsFragment() {
    // Required empty public constructor
}

@Override
public void onAttach(Context context)
{
    filterGameTitle = ((MainActivity)getActivity()).filterGameTitle;
    filterGenreId = ((MainActivity)getActivity()).filterGenreId;

    super.onAttach(context);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View searchResultsView = inflater.inflate(R.layout.fragment_search_results, container, false);

    return searchResultsView;
}

private void findViews()
{
    infoTextView = getActivity().findViewById(R.id.info_textView);
    listViewGames = getActivity().findViewById(R.id.games_listView);
}


}
113408
  • 3,364
  • 6
  • 27
  • 54
  • @TasosKaratzoglou check my update to the **SearchResultFragment** I used the onAttach method to make sure I don't access the activity until the fragment is attached to it. – 113408 Feb 12 '19 at 13:14
  • OnAttach android studio says it is deprecated – Tasos Karatzoglou Feb 12 '19 at 13:35
  • Check the new onAttach – 113408 Feb 12 '19 at 13:44
  • You have Skype or mail to send you my Private GitHub repo? It does not seem to work. I am really stuck now. I don't seem to find a way to get the search work. I my goal to perform search on json data and show results in a listview or recycleview – Tasos Karatzoglou Feb 12 '19 at 21:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188304/discussion-between-tasos-karatzoglou-and-113408). – Tasos Karatzoglou Feb 12 '19 at 21:34