ERRO NO REPOSITORIO (ANDROID)
OI PESSOAL FIZ A CLASSE REPOSITOTIO SO Q STA DANDO ERRO EM 3 LUGARES UM E NA LINHA valores.put(CAMPOS_CLIENTE[5], f.getUf()); ESTOU USANDO UM LIST, NAO ESTOU SABENDO COMO COLOCA-LO,
A OUTRA E NA LINHA deuErro = database.update(DBHelper.Tabela_Cliente, valores, CAMPOS_CLIENTE[0]
+ = ?, new String[] { String.valueOf(f.getIdCliente()))) == 0; STA DANDO UM ERRO DE COMPATIBILIDAD
E NESSA LINHA Cliente cliente = new Cliente(_idCliente, nome, endereco, bairro, cidade, uf, d_nascimento, cep, cpf, email, login, senha);
A OUTRA E NA LINHA deuErro = database.update(DBHelper.Tabela_Cliente, valores, CAMPOS_CLIENTE[0]
+ = ?, new String[] { String.valueOf(f.getIdCliente()))) == 0; STA DANDO UM ERRO DE COMPATIBILIDAD
E NESSA LINHA Cliente cliente = new Cliente(_idCliente, nome, endereco, bairro, cidade, uf, d_nascimento, cep, cpf, email, login, senha);
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dao;
import classes.Cliente;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import classes.Produto;
import conexao.DBHelper;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Peter
*/
public class RepositorioCliente {
private static final String[] CAMPOS_CLIENTE = {_idCliente, nome,endereco, bairro, cidade, uf, d_nascimento, cep, cpf, email, login, senha};
private static final String[] CAMPOS_PRODUTO = { _idProduto, produtoNome,
qtde, preco };
private SQLiteDatabase database;
//public RepositorioCliente(Context context) {
// DBHelper = new SqliteHelper(context);
//}
private static RepositorioCliente instance = new RepositorioCliente();
public static RepositorioCliente getInstance(Context ctx) {
if (instance.database == null || !instance.database.isOpen()) {
instance.database = new DBHelper(ctx).getWritableDatabase();
}
return instance;
}
private RepositorioCliente() {
}
public long salvar(Cliente cliente) {
int id = cliente.getIdCliente();
if (id == 0) {
return inserir(cliente);
} else {
return atualizar(cliente);
}
}
private long inserir(Cliente f) {
long _idCliente = -1;
database.beginTransaction();
try {
ContentValues valores = new ContentValues();
valores.put(CAMPOS_CLIENTE[1], f.getNome());
valores.put(CAMPOS_CLIENTE[2], f.getEndereco());
valores.put(CAMPOS_CLIENTE[3], f.getBairro());
valores.put(CAMPOS_CLIENTE[4], f.getCidade());
valores.put(CAMPOS_CLIENTE[5], f.getUf());
valores.put(CAMPOS_CLIENTE[6], f.getData_nasc());
valores.put(CAMPOS_CLIENTE[7], f.getCep());
valores.put(CAMPOS_CLIENTE[8], f.getCpf());
valores.put(CAMPOS_CLIENTE[9], f.getEmail());
valores.put(CAMPOS_CLIENTE[10], f.getLogin());
valores.put(CAMPOS_CLIENTE[11], f.getSenha());
_idCliente = database.insert(DBHelper.Tabela_Cliente, null, valores);
if (_idCliente != -1) {
boolean deuErro = false;
for (Produto produto : f.getProdutos()) {
ContentValues ivalores = new ContentValues();
ivalores.put(CAMPOS_PRODUTO[0], _idCliente);
ivalores.put(CAMPOS_PRODUTO[1], produto.getProdutoNome());
ivalores.put(CAMPOS_PRODUTO[2], produto.getQtde());
ivalores.put(CAMPOS_PRODUTO[3], produto.getPreco());
if (database.insert(DBHelper.Tabela_Produto , null, ivalores) == -1) {
deuErro = true;
break;
}
}
if (!deuErro) {
database.setTransactionSuccessful();
}
}
} finally {
database.endTransaction();
}
return _idCliente;
}
private long atualizar(Cliente f) {
boolean deuErro = false;
database.beginTransaction();
try {
ContentValues valores = new ContentValues();
valores.put(CAMPOS_CLIENTE[1], f.getNome());
deuErro = database.update(DBHelper.Tabela_Cliente, valores, CAMPOS_CLIENTE[0]
+ = ?, new String[] { String.valueOf(f.getIdCliente()))) == 0;
if (!deuErro) {
database.delete(DBHelper.Tabela_Produto,
CAMPOS_PRODUTO[0] + = + f.getIdCliente(), null);
for (Produto produto: f.getProdutos()) {
ContentValues ivalores = new ContentValues();
ivalores.put(CAMPOS_PRODUTO[0], f.getIdCliente());
ivalores.put(CAMPOS_PRODUTO[1], produto.getProdutoNome());
ivalores.put(CAMPOS_PRODUTO[2], produto.getQtde());
ivalores.put(CAMPOS_PRODUTO[3], produto.getPreco());
if (database.insert(DBHelper.Tabela_Produto, null, ivalores) == -1) {
deuErro = true;
break;
}
}
if (!deuErro) {
database.setTransactionSuccessful();
}
}
} finally {
database.endTransaction();
}
return f.getIdCliente();
}
public void excluir(int id) {
database.beginTransaction();
try {
database.delete(DBHelper.Tabela_Produto, CAMPOS_PRODUTO[0] + =?,
new String[] { String.valueOf(id) });
database.delete(DBHelper.Tabela_Cliente, CAMPOS_PRODUTO[0] + =?,
new String[] { String.valueOf(id) });
database.setTransactionSuccessful();
} finally {
database.endTransaction();
}
}
public Cliente getCliente(int id) {
Cursor c = database.query(DBHelper.Tabela_Cliente, null, CAMPOS_CLIENTE[0] + =
+ id, null, null, null, null);
if (c.getCount() > 0) {
c.moveToFirst();
return fillFeira(c);
}
return null;
}
private Cliente fillFeira(Cursor c) {
int id = c.getInt(c.getColumnIndex(CAMPOS_CLIENTE[0]));
String nome = c.getString(c.getColumnIndex(CAMPOS_CLIENTE[1]));
Cliente cliente = new Cliente(_idCliente, nome, endereco, bairro, cidade, uf, d_nascimento, cep, cpf, email, login, senha);
Cursor ic = database.query(DBHelper.Tabela_Produto, null, _idProduto = ?,
new String[] { String.valueOf(id) }, null, null, null);
ic.moveToFirst();
while (!ic.isAfterLast()) {
cliente.getProdutos().add(fillProduto(ic));
ic.moveToNext();
}
return cliente;
}
private Produto fillProduto(Cursor c) {
String produtoNome = c.getString(c.getColumnIndex(CAMPOS_PRODUTO[1]));
double qtde = c.getDouble(c.getColumnIndex(CAMPOS_PRODUTO[2]));
double preco = c.getDouble(c.getColumnIndex(CAMPOS_PRODUTO[3]));
return new Produto(produtoNome, qtde, preco);
}
public List<Cliente> listaCliente() {
List<Cliente> lista = new ArrayList<Cliente>();
Cursor c = database.query(DBHelper.Tabela_Cliente, null, null, null, null, null,
null);
c.moveToFirst();
while (!c.isAfterLast()) {
Cliente cliente = fillFeira(c);
lista.add(cliente);
c.moveToNext();
}
return lista;
}
}
Lu
Curtidas 0