package com.example.pcpower.agenda.dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.pcpower.agenda.modelo.Cliente;
import java.util.ArrayList;
import java.util.List;
/**
* Created by PcPower on 22/07/2018.
*/
public class ClienteDAO extends SQLiteOpenHelper {
public ClienteDAO(Context context) {
super(context, "Agenda", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE CLIENTES (id INTEGER PRIMARY KEY, cliente TEXT NOT NULL, largura TEXT, nota REAL);";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS Clientes";
db.execSQL(sql);
onCreate(db);
}
public void insere(Cliente cliente) {
SQLiteDatabase db = getWritableDatabase();
ContentValues dados = new ContentValues();
dados.put("cliente", cliente.getCliente());
dados.put("largura", cliente.getLargura());
dados.put("nota", cliente.getNota());
db.insert("Clientes", null, dados);
}
public List<Cliente> buscaclientes() {
String sql = "SELECT * FROM Clientes;";
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.rawQuery(sql, null);
List<Cliente> clientes = new ArrayList<Cliente>();
while (c.moveToNext()) {
Cliente cliente = new Cliente();
cliente.setId(c.getLong(c.getColumnIndex("id")));
cliente.setCliente(c.getString(c.getColumnIndex("cliente")));
cliente.setLargura(c.getString(c.getColumnIndex("largura")));
cliente.setNota(c.getDouble(c.getColumnIndex("nota")));
clientes.add(cliente);
}
c.close();
return null;
}
}
package com.example.pcpower.agenda.modelo;
/**
* Created by PcPower on 22/07/2018.
*/
public class Cliente {
private Long id;
private String cliente;
private String largura;
private Double nota;
public Double getNota() {
return nota;
}
public void setNota(Double nota) {
this.nota = nota;
}
public String getLargura() {
return largura;
}
public void setLargura(String largura) {
this.largura = largura;
}
public String getCliente() {
return cliente;
}
public void setCliente(String cliente) {
this.cliente = cliente;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public String toString() {
return getId() + " - " + getCliente();
}
}
package com.example.pcpower.agenda;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.pcpower.agenda.dao.ClienteDAO;
import com.example.pcpower.agenda.modelo.Cliente;
public class FormularioActivity extends AppCompatActivity {
private FormularioHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_formulario);
helper = new FormularioHelper(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_formulario,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_ok:
Cliente cliente = helper.pegacliente();
ClienteDAO dao = new ClienteDAO(this);
dao.insere(cliente);
dao.close();
Toast.makeText(FormularioActivity.this, "Cliente " + cliente.getCliente()+ " Salvo!", Toast.LENGTH_SHORT).show();
finish();
break;
}
return super.onOptionsItemSelected(item);
}
}
Em Resposta coloquei o resto do codigo