在Spinner上选择的项目是全局的 - Android

时间:2016-02-20 18:19:37

标签: android spinner global

我有两个活动,其中" SecondActivity"有一个Spinner,Spinner上的选定项目被发送到" TerceiraActivity"

但是,我想让Spinner上选择的项目变为全局变量,所以我将这个变量放在其他类中,称为" BD.class"

这是" 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) {

}
      }

这是&#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;

公共类TerceiraActivity扩展了Activity {

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


}


    }

这是&#34; BD.class&#34;

package br.exemplosqlite;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

 public class BD {
private SQLiteDatabase bd;

public BD(Context context){
    BDCore auxBd = new BDCore(context);
    bd = auxBd.getWritableDatabase();
}


public void inserir(Produtos produtos){
    ContentValues valores = new ContentValues();
    valores.put("item", produtos.getItem());
    valores.put("coligada", produtos.getColigada());
    valores.put("filial", produtos.getFilial());


    bd.insert("produtos2", null, valores);
}


public void atualizar(Produtos produtos){
    ContentValues valores = new ContentValues();

    valores.put("item", produtos.getItem());
    valores.put("coligada", produtos.getColigada());
    valores.put("filial", produtos.getFilial());

    bd.update("produtos2", valores, "_id = ?", new String[]{""+produtos.getId()});
}


public void deletar(Produtos produtos){
    bd.delete("produtos2", "_id = "+produtos.getId(), null);
}


public List<Produtos> buscar(){
    List<Produtos> list = new ArrayList<Produtos>();
    String[] colunas = new String[]{"_id", "item", "coligada","filial"};

    Cursor cursor = bd.rawQuery("select * from produtos2",null);

    if(cursor.getCount() > 0){
        cursor.moveToFirst();

        do{

            Produtos p = new Produtos();
            p.setId(cursor.getLong(0));
            p.setItem(cursor.getString(1));
            p.setColigada(cursor.getString(2));
            p.setFilial(cursor.getString(3));
            list.add(p);

        }while(cursor.moveToNext());
    }

    return(list);
}
             }

2 个答案:

答案 0 :(得分:0)

您可以将其存储在共享首选项中。只需将以下代码复制到名为AppSharedPreferences的新类中

public class AppSharedPreferences {
private SharedPreferences appSharedPrefs;
private SharedPreferences.Editor prefsEditor;
private static AppSharedPreferences appSharedPrefrence;

public AppSharedPreferences(Context context) {
    this.appSharedPrefs = context.getSharedPreferences("sharedpref", Context.MODE_PRIVATE);
    this.prefsEditor = appSharedPrefs.edit();
}

public static AppSharedPreferences getsharedprefInstance(Context con) {
    if (appSharedPrefrence == null)
        appSharedPrefrence = new AppSharedPreferences(con);
    return appSharedPrefrence;
}

public SharedPreferences getAppSharedPrefs() {
    return appSharedPrefs;
}

public void setAppSharedPrefs(SharedPreferences appSharedPrefs) {
    this.appSharedPrefs = appSharedPrefs;
}

public SharedPreferences.Editor getPrefsEditor() {
    return prefsEditor;
}

public void Commit() {
    prefsEditor.commit();
}

public void clearallSharedPrefernce() {
    prefsEditor.clear();
    prefsEditor.apply();
}
public void setSession(String session)
{
    this.prefsEditor=appSharedPrefs.edit();
    prefsEditor.putString("session",session);
    prefsEditor.apply();
}
public String getSession()
{
    return appSharedPrefs.getString("session","");
}

}

并在你的类中使用它创建它的对象就像这样 首先定义一个这样的对象

AppSharedPreferences appSharedPreferences;

然后像这样实例化

appSharedPreferences = AppSharedPreferences.getsharedprefInstance(YourActivityName.this);

然后保存值调用setter方法

appSharedPreferences.setSession(your value);

并类似地使用getter方法获取值

答案 1 :(得分:0)

我找到了解决方案,创建了一个Global Class,并分配了我的Spinner.toString()的值;

谢谢大家:D