Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid passing variables between activities
    primarykey
    data
    text
    <p>I want to pass one or more variables between activities in android. One solution is to use global variables, and it is explained here: <a href="https://stackoverflow.com/questions/1944656/android-global-variable">Android global variable</a> which I repeat here:</p> <hr> <p>You can extend the base android.app.Application class and add member variables like so:</p> <pre><code>public class MyApplication extends Application { private String someVariable; public String getSomeVariable() { return someVariable; } public void setSomeVariable(String someVariable) { this.someVariable = someVariable; } } </code></pre> <p>Then in your activities you can get and set the variable like so:</p> <pre><code>// set ((MyApplication) this.getApplication()).setSomeVariable("foo"); // get String s = ((MyApplication) this.getApplication()).getSomeVariable(); </code></pre> <hr> <p>I want to use this and set a global variable in one of my activities when an item on a ListView is pressed. So I have this code:</p> <pre><code> lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) { . . . ((MyApplication) this.getApplication()).setSomeVariable("foo"); } }); </code></pre> <p>However I get an error which says "The method getApplication() is undefined for the type new AdapterView.OnItemClickListener(){}".</p> <p>I would like to know what is the reason I get this error and how to fix it, or a better way of passing a variable from one activity to the other.</p> <p>Thanks,</p> <p>TJ</p>
    singulars
    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