package com.example.myapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private SQLiteDatabase db = null;

    private EditText etNome = null;
    private EditText etMostra = null;
    private Button btInsere = null;
    private Button btMostra = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etNome = (EditText) findViewById(R.id.et_nome);
        etMostra = (EditText) findViewById(R.id.et_mostra);

        btInsere = (Button) findViewById(R.id.bt_insere);
        btInsere.setOnClickListener(this);

        btMostra = (Button) findViewById(R.id.bt_mostra);
        btMostra.setOnClickListener(this);

        db = new MyDatabaseManager(this, "BancoDados", null, 1).getWritableDatabase();

    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.bt_insere:

                ContentValues dados = new ContentValues();
                dados.put("nome", etNome.getText().toString());
                dados.put("fone", "1312");
                dados.put("endereco", "algum endereco");

                db.insert("agenda", null, dados);

                //db.update("agenda", dados, "id=?", new String[]{valorASerPassado});
                //db.delete("agenda", "id=?", new String[]{valorASerPassado});

                break;
            case R.id.bt_mostra:

                etMostra.setText("");
                Cursor cur = db.query("agenda", new String[]{"id", " nome", "fone", "endereco"}, null, null, null, null, null);
                while(cur.moveToNext()){
                    etMostra.append(cur.getString(0) + " " + cur.getString(1) + "\n");
                }


                break;
        }
    }
}