1

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?

4 Answers4

1

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.

Lester L.
  • 959
  • 1
  • 9
  • 18
  • yes , like " select * from produtos2 where coligada = 'this is the item selected from spinner " – Kamikaze Stifler Feb 19 '16 at 06:03
  • Just look at my edited answer hope that answer will help you. – Lester L. Feb 19 '16 at 07:20
  • Hey Lester , i think that i'll work ... but , my BD.class dont have " Oncreate " . i 'ill post the full BD.class code friend. – Kamikaze Stifler Feb 19 '16 at 15:39
  • How many activity do you have ? And list it all . I think **Filtro_Activity** and **BD** is the activity you're talking ? – Lester L. Feb 19 '16 at 15:59
  • Okay , i'll list all. one moment friend. – Kamikaze Stifler Feb 19 '16 at 16:08
  • Okay , posted all class. – Kamikaze Stifler Feb 19 '16 at 16:58
  • from what activity do you want to pass a data ? do like this **activity_name1** to **activity_name2** – Lester L. Feb 19 '16 at 17:06
  • i want choose a item from spinner at "Filtro_Activitty" , and the item that a choose go to "BD.class " at code " Cursor cursor = bd.rawQuery("select * from produtos2 where coligada =" +**this i wanna be the item selected from spinner**, null); " . after this , i'll be displayed the itens , the activity that show the itens are " ListUserActivity1 " – Kamikaze Stifler Feb 19 '16 at 17:11
  • Oh so don't extend Activity to your BD class. – Lester L. Feb 19 '16 at 17:52
  • Okay lester , i 'ill try now my friend. – Kamikaze Stifler Feb 19 '16 at 18:09
  • Check the updated code above **Updated Answer check below start to this line** start below this line – Lester L. Feb 19 '16 at 18:11
  • I made the changes , but , now at " NewUserActivity " is giving the error . ' Error:(64, 11) error: constructor BD in class BD cannot be applied to given types; required: Context,String found: NewUserActivity reason: actual and formal argument lists differ in length ' where have ' BD bd = new BD(this); " the ('this') is red . – Kamikaze Stifler Feb 19 '16 at 18:58
  • in your NewUserActivity onCreate() put this code `Bundle extras = getIntent().getExtras(); String coligada = extras.getString("coligada"); List list = BD.newInstance(getApplicationContext(), coligada).buscar2(); setListAdapter(new ProdutosAdapter(this, list));` make sure that you never miss any word. – Lester L. Feb 19 '16 at 20:17
  • yes , lets make this code run , so .. i made the changes , i 'll update "NewUserActivity" code to you see , but keeping showing error : "Error:(37, 3) error: cannot find symbol method setListAdapter(ProdutosAdapter)" – Kamikaze Stifler Feb 19 '16 at 20:40
  • Maybe your layout R.layout.activity_new_user is not using ListView. – Lester L. Feb 19 '16 at 20:51
  • i'll post too. but NewUserActivity dont have ListView. – Kamikaze Stifler Feb 19 '16 at 20:59
  • Hey Lester , some news ? :D – Kamikaze Stifler Feb 20 '16 at 00:39
  • Oh I knew it you are using setListAdapter without having a listview inside your layout, and you did not extend your activity to ListActivity. Just google how to use ListView it will return you the answer. Thanks. – Lester L. Feb 20 '16 at 06:41
  • Thanks Lester , i think that making a variable public static , and the value be the ItemSelected from spinner , can work , could u help me with this ? – Kamikaze Stifler Feb 20 '16 at 06:49
  • No. You are not doing the right coding standard if you do that. The problem in your code is the NewUserActivity and your layout. Just focus on that. – Lester L. Feb 20 '16 at 06:52
  • Just google how to use ListView it will return you the answer. Thanks – Lester L. Feb 20 '16 at 06:54
  • Lester , i read about ListView , and so i could understand that is create a list of values populating layout withou need a .xml , right ? – Kamikaze Stifler Feb 20 '16 at 20:40
0

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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Vishal Thakkar
  • 2,117
  • 2
  • 16
  • 33
0

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.

Roy Falk
  • 1,685
  • 3
  • 19
  • 45
0

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/