Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to access variable inside listener
    primarykey
    data
    text
    <p>I have a listener excerpt below which contains the variable degree.</p> <pre><code>public class CustomOnItemSelectedListener implements AdapterView.OnItemSelectedListener { //Define Components public EditText text; public Spinner spinner1; //Define Variables public String degree; public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int pos,long id) { //Set the selected item on spinner1 to the variable tempValue String tempValue = spinner1.getSelectedItem().toString(); degree = "jobby"; } </code></pre> <p>I am trying to access that variable within my ViewPager adapter like so:</p> <pre><code>public class MyPagerAdapter extends FragmentPagerAdapter { public CustomOnItemSelectedListener selectedListener; [...] @Override public Fragment getItem(int pos) { switch(pos) { case 0: return resultFragment.newInstance(selectedListener.degree); case 1: return resultFragment.newInstance("resultFragment, Instance 2"); default: return resultFragment.newInstance("resultFragment, Default"); } } [...] } </code></pre> <p>And in my MainActivity</p> <pre><code>protected void onCreate(Bundle savedInstanceState) { [...] myPager = new MyPagerAdapter(getSupportFragmentManager()); selectedListener = new CustomOnItemSelectedListener(); addListenerOnSpinnerItemSelection(); myPager.selectedListener = selectedListener; } </code></pre> <p>The issue I am having is that I am unable to access the variable of degree. If I set the value of the variable at the top of the listener class like so</p> <pre><code>public String degree = "jobby"; </code></pre> <p>Then I can access the variable.</p> <p>I am trying to write an if statement within the listener, and depending on which item of the spinner is selected, the variable changes.</p> <p>How can I access the variable within the listener class?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload