Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

java - How do I get a variable in another activity?

How do I access variable value in another activity. In my example I have a string variable item which value is spinner selected value. How can I access this variable in another activity without using Intent?

  public class LoginScreen extends Activity {

      Spinner sp;
String item;


      Spinner sp = (Spinner) findViewById(R.id.lgnspinner);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.network_array,
            android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    sp.setAdapter(adapter);

    sp.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            item = (String) parent.getItemAtPosition(position);



        public class AgAppMenu extends Activity {
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can declare them as static variables and then in your other class you may access them like Activity1.stringName.

public static String stringName; 

stringName = .. // value from Spinner

Then, in all the other Activities, you can access them as YourMainActivty.stringName.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...