我想将第一个活动中的微调器组件的值传递给第二个活动中的sqlite查询。通过Spinner实现价值。
第一项活动 - 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) {
}
}
另一项活动是:
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);
}
如您所见,第二个活动有一个db.query,我想用第一个活动的微调器值修改它。这是可能的,最好的方法是什么?
答案 0 :(得分:1)
您是否想通过选择coligada的值来使用代码中的where子句?
编辑答案
Filtro_Activity 中的
像这样编辑你的确定按钮
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);
}
});
在onCreate(Bundle savedInstanceState)中的 BD类/活动中;在setContentView之后放了这个
Bundle extras = getIntent().getExtras();
String coligada = extras.getString("coligada");
List<Produtos> list = buscar2(coligada);
coligada字符串像这样传递给这个函数
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);
}
更新后的答案检查从此行开始
更新 BD Class ,如下面的代码
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);
}
}
Filtro_Activity 中的
像这样编辑你的确定按钮
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);
}
});
ListUserActivity1.class 中 onCreate 内的将此代码放在setContentView(); 之后
Bundle extras = getIntent().getExtras();
String coligada = extras.getString("coligada");
List<Produtos> list = BD.newInstance(getApplicationContext(), coligada).buscar2();
希望这对你有帮助。
答案 1 :(得分:0)
就在您的BD类上,您必须从第一个Activity传递的意图中获得值
主要的是你将意图中的价值传递给其他活动非BD,因此你无法在该活动中获得它
在fitro_activity中替换此行
Intent intent=new Intent(this,BD.class);intent.putExtra("coligada","Your Spinner selected Value");startActivity(intent);
在你的BD Class On Create中写这个
Intent i = getIntent(); String spn1 = i.getString("coligada");
您在fit_Activity的putextra中设置的任何值,使用&#34; coligada&#34;你在sp1 string中得到它。你可以使用toast或Log
上的print来检查它答案 2 :(得分:0)
我假设你真的想在DB中的Filtro_Activity中使用微调器的值。看起来你几乎就在那里。
这一行:
//chamada para a nova Activity
Intent intent = new Intent(Filtro_Activity.this, ListUsersActivity1.class);
应该是:
//chamada para a nova Activity
Intent intent = new Intent(Filtro_Activity.this, DB.class);
使用要将数据发送到的数据库活动。请注意,它可能不应该是一个活动,而是一个从Filtro_Activity实例化的简单类。
&#34;活动是用户可以做的一件重点事。几乎所有活动都与用户互动......&#34;与DB的后台连接并不合适,而且活动通常具有我在DB中看不到的GUI。
这个答案确实偏离了原始问题,所以如果你真的想发送微调器数据,你可以在第二个活动onCreate方法中运行以下代码。
Intent intent = getIntent();
String coligada = intent.getStringExtra("coligada");
这些来自http://developer.android.com/training/basics/firstapp/starting-activity.html
的安卓教程顺便说一句,你应该使用style guides for Java。
答案 3 :(得分:0)
要将信息传输到另一个活动,请使用用于启动新活动的intent。以下是示例链接http://www.hackpundit.com/android-transfer-data-activity-intent/