简体   繁体   English

将数据从一项活动转移到另一项活动

[英]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. 我想将第一个活动中微调器组件的值传递给第二个活动中的sqlite查询。 going the value throught a Spinner. 通过微调器获得价值。

First Activity - Filtro_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. 如您所见,第二个活动有一个db.query,我想使用第一个活动中的微调器值进行修改。 Is this possible and what would be the best way to go about it? 这可能吗,什么是最好的方法?

Do you want to use the where clause in your code by selecting the value of coligada ? 您是否要通过选择coligada的值在代码中使用where子句?

Edit answer 编辑答案

in your Filtro_Activity 在您的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); 在您的BD类/活动中的 onCreate(Bundle savedInstanceState)内; after setContentView put this setContentView之后放这个

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

the coligada string pass to this function like this 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);
}

Updated Answer check below start to this line 下面的更新的答案检查开始于此行

update your BD Class like the code below 像下面的代码一样更新您的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);
}

}

in your Filtro_Activity 在您的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(); ListUserActivity1.classonCreate内部,将此代码放在setContentView()之后;

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

Hope this helps you now. 希望这对您有所帮助。

just on your BD Class you have to get Value from intent which You pass from first Activity like this 就在您的BD类上,您必须从意图中获得价值,而意图是您从这样的第一个活动中传递的

And Main thing is that you pass value in intent to other Activity Not BD so you cant get it in that Activity 最主要的是,您将意图的价值传递给其他活动而非BD,因此您无法在该活动中获得它

replace This line in fitro_activity 在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 在您创建的BD类中写上

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 无论您在fitroActivity的putextra中使用“ coligada”键设置的值如何,都可以在sp1字符串中获取它。可以在Toast或Log上使用print进行检查

I'm assuming you really wanted to use the values of the spinners in Filtro_Activity in DB. 我假设您真的想在DB的Filtro_Activity中使用微调器的值。 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. 提醒您,它可能不应该是一项活动,而是可以从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. “活动是用户可以做的一件专注的事情。几乎所有活动都与用户进行交互……”与数据库的后台连接确实不适合,活动通常具有在DB中看不到的GUI 。

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. 这个答案确实偏离了原始问题,因此,如果您确实想发送微调器数据,则可以在第二个活动onCreate方法中运行以下代码。

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 这些来自位于http://developer.android.com/training/basics/firstapp/starting-activity.html的android教程。

By the way, the there are style guides for Java which you should probably use. 顺便说一下,您可能应该使用Java的样式指南

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/ 以下是一个示例链接http://www.hackpundit.com/android-transfer-data-activity-intent/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从一个活动转移到另一个活动? - How to transfer data from one activity to another? 使用意图将数据从一个活动传输到另一个活动 - Transfer data from one Activity to Another Activity Using Intents 从一项活动转移到另一项活动 - Transfer from one activity to another 如何通过活动将额外数据从一个片段传输到另一个片段 - how to transfer extra data from one fragment to another through activity 如何将数据从一个活动转移到另一个活动并将其设置为textview? - How to transfer data from one activity to another and set it to the textview? 如何将变量的数据从一个活动转移到另一个活动 - How to transfer the data of a variable from one Activity to another 如何使用startChildActivity将数据从一个子活动传输到另一个子活动? - how to transfer data from one child activity to another using startChildActivity? 如何在Android中将数据从一项活动转移到另一项活动? - How do I transfer data from one activity to another in Android? 如何在android中将数据从一个活动传输到另一个活动 - How to transfer data from one activity to another in android 试图在 android 中将数据从一个活动传输到另一个活动,但它在另一个活动上显示 null 值 - Trying to transfer data from one activity to another in android but it show null value on another activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM