Kamikaze Stifler
Kamikaze Stifler

Reputation: 109

Transfer data from one activity to another

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?

Upvotes: 1

Views: 305

Answers (4)

Vishal Thakkar
Vishal Thakkar

Reputation: 2127

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

Upvotes: 0

Thomas Jacob
Thomas Jacob

Reputation: 56

For transferring info to another activity use the intent which is used to start the new activity. Below is a sample link http://www.hackpundit.com/android-transfer-data-activity-intent/

Upvotes: 0

Lester L.
Lester L.

Reputation: 969

Do you want to use the where clause in your code by selecting the value of coligada ?

Edit answer

in your Filtro_Activity

edit your ok button like this

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, BD.class);
        Bundle extras = new Bundle();
        extras.putString("coligada", spcoligada.getSelectedItem().toString());
        intent.putExtras(extras);
        startActivity(intent);
    }
});

in your BD class/activity inside onCreate(Bundle savedInstanceState); after setContentView put this

Bundle extras = getIntent().getExtras();
String coligada = extras.getString("coligada");
List<Produtos> list = buscar2(coligada);

the coligada string pass to this function like this

public List<Produtos> buscar2(String coligada) {

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 where coligada = ? ", new String[] {coligada});
//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());
}
cursor.close()
return (list);
}

Updated Answer check below start to this line

update your BD Class like the code below

public class BD {

    private SQLiteDatabase bd;
    private String coligada;

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

    public static BD newInstance(Context context, String coligada){
        return new BD(context, coligada);
    }

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 where coligada = ? ", new String[] {coligada});
//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());
    }
    cursor.close()
    return (list);
}

}

in your Filtro_Activity

edit your ok button like this

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);
        Bundle extras = new Bundle();
        extras.putString("coligada", spcoligada.getSelectedItem().toString());
        intent.putExtras(extras);
        startActivity(intent);
    }
});

inside your onCreate from the ListUserActivity1.class put this code after setContentView();

Bundle extras = getIntent().getExtras();
String coligada = extras.getString("coligada");
List<Produtos> list = BD.newInstance(getApplicationContext(), coligada).buscar2();

Hope this helps you now.

Upvotes: 1

Roy Falk
Roy Falk

Reputation: 1741

I'm assuming you really wanted to use the values of the spinners in Filtro_Activity in DB. It looks like you're almost there.

This line:

//chamada para a nova Activity
Intent intent = new Intent(Filtro_Activity.this, ListUsersActivity1.class);

should really be:

//chamada para a nova Activity
Intent intent = new Intent(Filtro_Activity.this, DB.class);

with the DB activity you want to send the data to. Mind you, it probably shouldn't be an activity but a simple class to be instantiated from Filtro_Activity.

"An activity is a single, focused thing that the user can do. Almost all activities interact with the user..." A background connection to the DB doesn't really fit and activities typically have GUI which I don't see in DB.

This answer really deviated from the original question, so if you really wanted to send the spinner data, you would run the following code in the second activity onCreate method.

Intent intent = getIntent();
String coligada = intent.getStringExtra("coligada");

These are from the android tutorial at http://developer.android.com/training/basics/firstapp/starting-activity.html

By the way, the there are style guides for Java which you should probably use.

Upvotes: 0

Related Questions