gpt4 book ai didi

android - 将数据从一项 Activity 传输到另一项 Activity

转载 作者:行者123 更新时间:2023-11-30 01:33:13 25 4
gpt4 key购买 nike

我想将第一个 Activity 中的微调器组件的值传输到第二个 Activity 中的 sqlite 查询。通过 Spinner 传递值。

第一个 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) {

}

另一个 Activity 是:

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);
}

如您所见,第二个 Activity 有一个 db.query,我想用第一个 Activity 的微调器值对其进行修改。这可能吗?最好的方法是什么?

最佳答案

您想通过选择 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);
}
});

在您的 BD 类/Activity 中 onCreate(Bundle savedInstanceState);在 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类

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.classonCreate 中,将此代码放在 setContentView() 之后;

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

希望现在对您有所帮助。

关于android - 将数据从一项 Activity 传输到另一项 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35497869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com