将Spinner的值发送到Public Static变量 - Android

时间:2016-02-20 06:04:55

标签: android android-studio android-intent android-activity spinner

所以..我有两个活动,SecondActivity我有一个Spinner,从这个微调器中选择的项目必须存储在一个变量公共静态,其中TerceiraActivity我可以得到变量操纵。

论文是活动。

"的 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.Spinner;

    import org.w3c.dom.Text;

    public class SecondActivity extends Activity implements  AdapterView.OnItemSelectedListener {

public static String mahone = "atemais";



    @Override
    protected 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());

                public String pcoligada="";








                startActivity(intent);
            }
        });
    }

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

    }

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

    }
       }

这是&#34; TerceiraActivity &#34;

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);
}
  }

1 个答案:

答案 0 :(得分:0)

只需将getSerializableExtra更改为getStringExtra

即可

如果你想将 pcoligada 公开,而不是声明为全局变量,

将代码更改为:

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);

public String pcoligada = ""; //获取全局变量,您也可以将其作为静态变量从其他类访问

另一项活动的onCreate()方法;

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

    pcoligada = intent.getStringExtra("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);