Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving difficulty with a global variable in android application
    primarykey
    data
    text
    <p>Hey I am having trouble declaring, changing, and using a global variable. I tried the whole "make a class that extends your application and put variables there" thing but I'm not sure how to implement it. Here is my class with the variable in it.</p> <pre><code> public class MyApp extends Application { public int listPos; } </code></pre> <p>I then tried to access and change int listPos here.</p> <pre><code>public class Browse extends ListActivity{ MyApp app = ((MyApp)getApplicationContext()); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] coffeeTypes = getResources().getStringArray(R.array.coffeeTypes); setListAdapter(new ArrayAdapter&lt;String&gt;(this, R.layout.listview, coffeeTypes)); ListView lv = getListView(); lv.setTextFilterEnabled(true); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) { app.listPos = position; startActivity(new Intent(Browse.this, CoffeeTypes.class)); } }); } </code></pre> <p>}</p> <p>Then I tried to access the variable in the following activity to determine the outcome of an if else statement</p> <pre><code>public class CoffeeTypes extends Activity{ MyApp app = ((MyApp)getApplicationContext()); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(app.listPos == 0){ Toast.makeText(this, "WEEEEEEE!0", Toast.LENGTH_LONG).show(); } else if(app.listPos == 1){ Toast.makeText(this, "RAWR!1", Toast.LENGTH_LONG).show(); </code></pre> <p>Anyone have any idea what I am doing wrong?</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.
    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