Item chosen in Spinner be a global variable

Asked

Viewed 416 times

1

Good afternoon, everyone ,

I have an Activity that has a Spinner , the value chosen in this Spinner is sent to another Activity in which I do getIntent and receive the value of Spinner , my doubt is, I would like to turn the received Item into a global variable , to use it in any Activity .

Follow the code of my Activitys.

Activity that sends the data :

"Secondactivity"

package br.exemplosqlite;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import org.w3c.dom.Text;

public class SecondActivity extends Activity implements AdapterView.OnItemSelectedListener {



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    //referencia a Spinner
    //Spinner coligada;

    //final TextView nome = (TextView)findViewById(R.id.txvNome);
    //final TextView sobrenome = (TextView)findViewById(R.id.txvSobrenome);
    //final Spinner pday = (Spinner)findViewById(R.id.spinner);

    final Spinner spcoligada = (Spinner)findViewById(R.id.coligada);








    //spinner = (Spinner)findViewById(R.id.spinner);

    ArrayAdapter adaptercoligada= ArrayAdapter.createFromResource(this, R.array.coligada, android.R.layout.simple_spinner_item);
    spcoligada.setAdapter(adaptercoligada);



    Button ok = (Button)findViewById(R.id.btnok);







    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //chamada para a nova Activity
            Intent intent = new Intent(SecondActivity.this, TerceiraActivity.class);
            intent.putExtra("coligada", spcoligada.getSelectedItem().toString());


            //intent.putExtra("nomePessoa", nome.getText().toString());
            //intent.putExtra("sobrenomePessoa", sobrenome.getText().toString());
            //intent.putExtra("day", pday.getSelectedItem().toString());










            startActivity(intent);
        }
    });
}




@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
    }

And this is the "Terceiraactivity" that receives the data.

package br.exemplosqlite;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Spinner;
import android.widget.TextView;

public class TerceiraActivity extends Activity {








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

    Intent intent = getIntent();
    //String parametro = (String) intent.getSerializableExtra("nomePessoa");
    //String psobrenome = (String) intent.getSerializableExtra("sobrenomePessoa");
    //String ppday = (String) intent.getSerializableExtra("day");

    String pcoligada = (String) intent.getSerializableExtra("coligada");











    //TextView nome = (TextView)findViewById(R.id.txvNome);
    //TextView sobrenome = (TextView)findViewById(R.id.txvSobrenome);
    TextView coligadas = (TextView)findViewById(R.id.spvcoligada);







    //nome.setText("Olá " + parametro + ", Tem de fazer a barba " );
    coligadas.setText("coliga escolhida : "+ pcoligada);


}


    }

2 answers

0


creates a global variabiel with the same name as the one you receive and, at the time of receiving, uses this.variável... so it becomes global...I don’t know if it will help. ex: declares a variable int i as global in the third Activity and at the time of receiving that value from the second, uses for example this. i = i; (the first i is of the third Activity and the second i is what you received of the second)

  • Oops, I followed that same line of thought of yours, I created a global class with the variables I want to be global. So I set the value in the second Activity " Global.class.vlor = related. And it’s working out, thanks friend

0

I suggest you two choices:

1st Create a class of variables static, so just call the name of the class.variavel, ex:

public class Globais{
    static String coligada;
}

2nd Storing in the memory of the mobile phone, if you want to save this variable even after closing the application, in this case it depends now on what you want to do in your application. For this we use the class SharedPreferences, ex:

Initialize

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

Receive

String storedPreference = preferences.getString("coligada", "valorPorDefeito");

Keep

SharedPreferences.Editor editor = preferences.edit();
editor.putString("coligada", pcoligada); // varivel a guardar
editor.commit();
  • Can be using the global class. The code above the global in which Activity I place it ?

  • Do not put in any Activity, you must create a new class with any name and the one inside defines its variables, then to use the variables in Activity just write the name of the class you created with a point and the name of the variable (Static), ex: Minhaclasse.variable = "test";

  • Right , something else , within this class that I will create and define the variables , how do I make these variables come from a Spinner ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.