I would like to transfer the value of a spinner component in the first activity to an sqlite query in the second activity. going the value throught a Spinner.
First Activity - Filtro_Activity
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;
public class Filtro_Activity extends Activity implements AdapterView.OnItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filtro_);
//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);
final Spinner spfilial = (Spinner)findViewById(R.id.filial);
final Spinner splestoque = (Spinner)findViewById(R.id.lestoque);
final Spinner spgprodutos = (Spinner)findViewById(R.id.gprodutos);
final Spinner spsubprodutos = (Spinner)findViewById(R.id.subproduto);
final Spinner spclprodutos = (Spinner)findViewById(R.id.clprodutos);
//spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter adaptercoligada=ArrayAdapter.createFromResource(this, R.array.coligada, android.R.layout.simple_spinner_item);
spcoligada.setAdapter(adaptercoligada);
ArrayAdapter adapterfilial=ArrayAdapter.createFromResource(this, R.array.filial, android.R.layout.simple_spinner_item);
spfilial.setAdapter(adapterfilial);
ArrayAdapter adapterlestoque=ArrayAdapter.createFromResource(this, R.array.lestoque, android.R.layout.simple_spinner_item);
splestoque.setAdapter(adapterlestoque);
ArrayAdapter adaptergprodutos=ArrayAdapter.createFromResource(this, R.array.gprodutos, android.R.layout.simple_spinner_item);
spgprodutos.setAdapter(adaptergprodutos);
ArrayAdapter adaptersubprodutos=ArrayAdapter.createFromResource(this, R.array.subproduto, android.R.layout.simple_spinner_item);
spsubprodutos.setAdapter(adaptersubprodutos);
ArrayAdapter adapterclprodutos=ArrayAdapter.createFromResource(this, R.array.clprodutos, android.R.layout.simple_spinner_item);
spclprodutos.setAdapter(adapterclprodutos);
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(Filtro_Activity.this, ListUsersActivity1.class);
intent.putExtra("coligada", spcoligada.getSelectedItem().toString());
intent.putExtra("filial", spfilial.getSelectedItem().toString());
intent.putExtra("lestoque", splestoque.getSelectedItem().toString());
intent.putExtra("gprodutos", spgprodutos.getSelectedItem().toString());
intent.putExtra("subprodutos", spsubprodutos.getSelectedItem().toString());
intent.putExtra("clprodutos", spclprodutos.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 the other activity is :
package br.exemplosqlite;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class BD extends Activity {
private SQLiteDatabase bd;
public BD(Context context) {
BDCore auxBd = new BDCore(context);
bd = auxBd.getWritableDatabase();
}
public List<Produtos> buscar2() {
List<Produtos> list = new ArrayList<Produtos>();
String[] colunas = new String[]{"_id","item", "coligada","filial"};
//String whereclausula = "coligada = 'issue'";
Cursor cursor = bd.rawQuery("select * from produtos2 ", null);
//Cursor cursor = bd.query("produtos", colunas,null, null, null, null,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);
}
As you can see, the second activity has a db.query which I would like to modify with the spinner values from the first activity. Is this possible and what would be the best way to go about it?
just on your BD Class you have to get Value from intent which You pass from first Activity like this
And Main thing is that you pass value in intent to other Activity Not BD so you cant get it in that Activity
replace This line in fitro_activity
Intent intent=new Intent(this,BD.class);intent.putExtra("coligada","Your Spinner selected Value");startActivity(intent);
In your BD Class On Create write this
Intent i = getIntent(); String spn1 = i.getString("coligada");
whatever Value you set in putextra of fitroActivity with key of "coligada" you get it in sp1 string.you can check it using print on Toast or Log
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments