Call database in another activity

2

Good afternoon, I created an application that counts the number of goals and the team name, and in the application has a button called result (in the menu) that opens another activity result, which has an area to put the name of the time and has to search, it shows the result and the name of the team ... but the problem is that I do not know how to call the database in the other activity, I made the database, and I made the call list to show the result but would need to call in the other activity and when you click the button it calls that database.

First screen Code:

public class FutebolSimples extends AppCompatActivity {

    private ImageButton imgButton_1, imgButton_2;
    private Button vermelho, amarelo;
    private TextView txt_valor1, txt_valor2;
    private EditText nomeTime1, nomeTime2;
    private int contador = 0;
    private int contador1 = 0;
    long tempoPausado = 0;

    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_futebol_simples);

        imgButton_1 = (ImageButton) findViewById(R.id.imgButton_1);
        imgButton_2 = (ImageButton) findViewById(R.id.imgButton_2);
        reloginho = (Chronometer) findViewById(R.id.chronometer);
        txt_valor1 = (TextView) findViewById(R.id.txt_valor1);
        txt_valor2 = (TextView) findViewById(R.id.txt_valor2);
        nomeTime1 = (EditText) findViewById(R.id.lbl_time1);
        nomeTime2 = (EditText) findViewById(R.id.lbl_time2);
        vermelho = (Button) findViewById(R.id.btnvermelho);
        amarelo = (Button) findViewById(R.id.btnAmarelo);

        imgButton_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contador++;
                txt_valor1.setText(" " + contador);
                Toast.makeText(getApplicationContext(), "Goooool!!!", Toast.LENGTH_LONG).show();
            }
        });

        imgButton_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contador1++;
                txt_valor2.setText(" " + contador1);
                Toast.makeText(getApplicationContext(), "Goooool!!!", Toast.LENGTH_LONG).show();
            }
        });

        db = openOrCreateDatabase("Resultado", Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS futebol (TimeOne VARCHAR, TimeTwo VARCHAR, FinalOne INT, FinalTwo INT);");

    }


    public void saveR (View view){

        if(txt_valor1.getText().toString().trim().length()==0 || txt_valor2.getText().toString().trim().length()==0 || nomeTime1.getText().toString().trim().length()==0 || nomeTime2.toString().trim().length()==0 || vermelho.getText().toString().trim().length()==0 || amarelo.getText().toString().trim().length()==0){

            Toast.makeText(getApplicationContext(), "Por favor inicia uma partida!", Toast.LENGTH_SHORT).show();

            return;
        }

        db.execSQL("INSERT INTO futebol VALUES('"+txt_valor1.getText()+"','"+txt_valor2.getText()+"','"+nomeTime1.getText()+"','"+nomeTime2.getText()+"','"+vermelho.getText()+"','"+amarelo.getText()+"');");

        Toast.makeText(getApplicationContext(), "Partida Salva", Toast.LENGTH_LONG).show();
    }

    public void listar (View view){
        Cursor c = db.rawQuery("SELECT * FROM futebol", null);

        if (c.getCount() == 0){
            Toast.makeText(getApplicationContext(), "Erro na pesquisa!", Toast.LENGTH_SHORT).show();
            return;
        }

        StringBuffer buffer = new StringBuffer();
        while (c.moveToFirst()) {

            buffer.append("Nome: "+c.getString(0)+"\n");
        }

        Toast.makeText(getApplicationContext(), "Resultado com sucesso!", Toast.LENGTH_SHORT);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.salvar) {

            return true;
        }

        if (id == R.id.result) {
            Intent mostrarResul = new Intent(this, ResultSimples.class);
            startActivity(mostrarResul);

            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

NOTE: Bank creation is in the same activity in the oncreate bundle ....

    
asked by anonymous 11.09.2016 / 02:15

1 answer

3

You can pass the data by parameter or access the database of the other activity:

Just call the same table

Use the same commands

 db = openOrCreateDatabase("Resultado", Context.MODE_PRIVATE, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS futebol (TimeOne VARCHAR, TimeTwo VARCHAR, FinalOne INT, FinalTwo INT);");

}


public void saveR (View view){

    if(txt_valor1.getText().toString().trim().length()==0 || txt_valor2.getText().toString().trim().length()==0 || nomeTime1.getText().toString().trim().length()==0 || nomeTime2.toString().trim().length()==0 || vermelho.getText().toString().trim().length()==0 || amarelo.getText().toString().trim().length()==0){

        Toast.makeText(getApplicationContext(), "Por favor inicia uma partida!", Toast.LENGTH_SHORT).show();

        return;
    }

    db.execSQL("INSERT INTO futebol VALUES('"+txt_valor1.getText()+"','"+txt_valor2.getText()+"','"+nomeTime1.getText()+"','"+nomeTime2.getText()+"','"+vermelho.getText()+"','"+amarelo.getText()+"');");

    Toast.makeText(getApplicationContext(), "Partida Salva", Toast.LENGTH_LONG).show();
}

public void listar (View view){
    Cursor c = db.rawQuery("SELECT * FROM futebol", null);

    if (c.getCount() == 0){
        Toast.makeText(getApplicationContext(), "Erro na pesquisa!", Toast.LENGTH_SHORT).show();
        return;
    }

    StringBuffer buffer = new StringBuffer();
    while (c.moveToFirst()) {

        buffer.append("Nome: "+c.getString(0)+"\n");
    }

    Toast.makeText(getApplicationContext(), "Resultado com sucesso!", Toast.LENGTH_SHORT);
}
    
11.09.2016 / 03:54