Calling up database in another Activity

Asked

Viewed 398 times

2

Good afternoon, I created an application that counts the number of goals and the name of the team, 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 team and send search, on that it shows the result and the name of the team... except that the problem is that I do not know how to call the database in the other Activity, I made the database, and made the call list to show the result but would need to call on the other Activity and when you click on the button it calls this 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: the creation of the bank is in the same Activity in oncreate Bundle....

1 answer

3


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

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);
}
  • Friend, when will open Activity it shows an error message "the application stopped working" why is this happening? was after putting the database....

  • In the second Activity? sends her code tbm, the first alone turns right

  • I already got here thanks for the help ....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.