I have a app working and need to implement a solution for the following class.
UsoPonta
long id
long idAmbiente
int[] usosPonta = new int[12];
DB structure
public static final String TABLE_USO_PONTA = "uso_ponta_ambientes";
public static final String COLUMN_USO_PONTA_ID = "_id";
public static final String COLUMN_USO_PONTA_AMBIENTE_ID = "id_ambiente";
public static final String COLUMN_USO_PONTA_1 = "uso_ponta_1";
public static final String COLUMN_USO_PONTA_2 = "uso_ponta_2";
public static final String COLUMN_USO_PONTA_3 = "uso_ponta_3";
public static final String COLUMN_USO_PONTA_4 = "uso_ponta_4";
public static final String COLUMN_USO_PONTA_5 = "uso_ponta_5";
public static final String COLUMN_USO_PONTA_6 = "uso_ponta_6";
public static final String COLUMN_USO_PONTA_7 = "uso_ponta_7";
public static final String COLUMN_USO_PONTA_8 = "uso_ponta_8";
public static final String COLUMN_USO_PONTA_9 = "uso_ponta_9";
public static final String COLUMN_USO_PONTA_10 = "uso_ponta_10";
public static final String COLUMN_USO_PONTA_11 = "uso_ponta_11";
public static final String COLUMN_USO_PONTA_12 = "uso_ponta_12";
In my working app I have 3 classes which are lists. nad all of them have it's respective DAO, Adapter, model and activities. Here are some example of Ambiente class.
Ambiente.java (Model)
package nermv.nermv15.model;
import java.io.Serializable;
/**
* Created by norsul on 11/04/2015.
*/
public class Ambiente implements Serializable{
private long id;
private long idUC;
private String nome;
private String area;
private String altura;
private String iluminanciaPre;
private String iluminanciaPos;
public Ambiente() {}
public Ambiente (long idUC, String nome, String area, String altura, String iluminanciaPre, String iluminanciaPos) {
this.idUC = idUC;
this.nome = nome;
this.area= area;
this.altura= altura;
this.iluminanciaPre= iluminanciaPre;
this.iluminanciaPos= iluminanciaPos;
}
public long pegaId()
{
return this.id;
}
public void setaId(long id)
{
this.id = id;
}
public long pegaIdUC()
{
return this.idUC;
}
public void setaIdUC(long idUC)
{
this.idUC = idUC;
}
public String pegaNome()
{
return this.nome;
}
public void setaNome(String nome)
{
this.nome = nome;
}
public String pegaArea()
{
return this.area;
}
public void setaArea(String area)
{
this.area = area;
}
public String pegaAltura()
{
return this.altura;
}
public void setaAltura(String altura) {this.altura = altura;}
public void setaIluminanciaPre(String iluminanciaPre) {this.iluminanciaPre = iluminanciaPre;}
public String pegaIluminanciaPre()
{
return this.iluminanciaPre;
}
public void setaIluminanciaPos(String iluminanciaPos) {this.iluminanciaPos = iluminanciaPos;}
public String pegaIluminanciaPos()
{
return this.iluminanciaPos;
}
}
AmbienteDAO.java (DAO)
package nermv.nermv15.dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import nermv.nermv15.model.Ambiente;
/**
* Created by norsul on 11/04/2015.
*/
public class AmbienteDAO {
public static final String TAG = "AmbienteDAO";
// campos do banco de dados
private SQLiteDatabase sqLiteDatabase;
private DbHelper dbHelper;
private Context context;
private String[] allColumns = { dbHelper.COLUMN_AMBIENTE_ID,
dbHelper.COLUMN_AMBIENTE_UC_ID, dbHelper.COLUMN_AMBIENTE_NOME,
dbHelper.COLUMN_AMBIENTE_AREA,
dbHelper.COLUMN_AMBIENTE_ALTURA,
dbHelper.COLUMN_AMBIENTE_ILUMINANCIA_PRE,
dbHelper.COLUMN_AMBIENTE_ILUMINANCIA_POS};
public Ambiente novoAmbiente(long idUC, String nome, String area,
String altura, String iluminanciaPre, String iluminanciaPos) {
ContentValues values = new ContentValues();
values.put(dbHelper.COLUMN_AMBIENTE_UC_ID, idUC);
values.put(dbHelper.COLUMN_AMBIENTE_NOME, nome);
values.put(dbHelper.COLUMN_AMBIENTE_AREA, area);
values.put(dbHelper.COLUMN_AMBIENTE_ALTURA, altura);
values.put(dbHelper.COLUMN_AMBIENTE_ILUMINANCIA_PRE, iluminanciaPre);
values.put(dbHelper.COLUMN_AMBIENTE_ILUMINANCIA_POS, iluminanciaPos);
long insertId = sqLiteDatabase
.insert(dbHelper.TABLE_AMBIENTES, null, values);
Cursor cursor = sqLiteDatabase.query(dbHelper.TABLE_AMBIENTES, allColumns,
dbHelper.COLUMN_AMBIENTE_ID + " = " + insertId, null, null,
null, null);
cursor.moveToFirst();
Ambiente novoAmbiente = cursorParaAmbiente(cursor);
cursor.close();
return novoAmbiente;
}
public List<Ambiente> pegaTodosAmbientes(long idUC) {
List<Ambiente> listUCs = new ArrayList<Ambiente>();
String[] matchValue = {String.valueOf(idUC)};
String whereClause = DbHelper.COLUMN_AMBIENTE_UC_ID + " = ?";
Cursor cursor = sqLiteDatabase.query(dbHelper.TABLE_AMBIENTES, allColumns,
whereClause, matchValue, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Ambiente ambiente = cursorParaAmbiente(cursor);
listUCs.add(ambiente);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
}
return listUCs;
}
public void editarAmbiente(long id, String nome, String area,
String altura, String iluminanciaPre, String iluminanciaPos) {
ContentValues values = new ContentValues();
values.put(dbHelper.COLUMN_AMBIENTE_ID, id);
values.put(dbHelper.COLUMN_AMBIENTE_NOME, nome);
values.put(dbHelper.COLUMN_AMBIENTE_AREA, area);
values.put(dbHelper.COLUMN_AMBIENTE_ALTURA, altura);
values.put(dbHelper.COLUMN_AMBIENTE_ILUMINANCIA_PRE, iluminanciaPre);
values.put(dbHelper.COLUMN_AMBIENTE_ILUMINANCIA_POS, iluminanciaPos);
sqLiteDatabase
.update(dbHelper.TABLE_AMBIENTES, values, "_id=" + id, null);
Cursor cursor = sqLiteDatabase.query(dbHelper.TABLE_AMBIENTES, allColumns,
dbHelper.COLUMN_UC_ID + " = "+ id, null, null,
null, null);
cursor.moveToFirst();
cursor.close();
}
public void excluirAmbiente(Ambiente ambiente) {
long id = ambiente.pegaId();
// delete all employees of this company
System.out.println("the deleted ambeinte has the id: " + id);
sqLiteDatabase.delete(dbHelper.TABLE_AMBIENTES, dbHelper.COLUMN_AMBIENTE_ID
+ " = " + id, null);
}
protected Ambiente cursorParaAmbiente(Cursor cursor) {
Ambiente ambiente = new Ambiente();
ambiente.setaId((cursor.getLong(0)));
ambiente.setaIdUC(cursor.getLong(1));
ambiente.setaNome(cursor.getString(2));
ambiente.setaArea(cursor.getString(3));
ambiente.setaAltura(cursor.getString(4));
ambiente.setaIluminanciaPre(cursor.getString(5));
ambiente.setaIluminanciaPos(cursor.getString(6));
return ambiente;
}
public AmbienteDAO(Context context) {
this.context = context;
dbHelper = new DbHelper(context);
// open the database
try {
open();
} catch (SQLException e) {
Log.e(TAG, "SQLException on openning database " + e.getMessage());
e.printStackTrace();
}
}
public void open() throws SQLException {
sqLiteDatabase = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
}
ListaAmbienteAdapter (Adapter)
package nermv.nermv15.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
import nermv.nermv15.R;
import nermv.nermv15.model.Ambiente;
/**
* Created by norsul on 09/04/2015.
*/
public class ListaAmbientesAdapter extends BaseAdapter
{
public static final String TAG = "ListAmbienteAdapter";
private List<Ambiente> mItems;
private LayoutInflater mInflater;
public ListaAmbientesAdapter(Context context, List<Ambiente> listAmbientes) {
this.setItems(listAmbientes);
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return (getItems() != null && !getItems().isEmpty()) ? getItems().size() : 0 ;
}
@Override
public Ambiente getItem(int position) {
return (getItems() != null && !getItems().isEmpty()) ? getItems().get(position) : null ;
}
@Override
public long getItemId(int position) {
return (getItems() != null && !getItems().isEmpty()) ? getItems().get(position).pegaId() : position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = mInflater.inflate(R.layout.lista_item_ambiente, parent, false);
holder = new ViewHolder();
holder.txtAmbienteNome = (TextView) convertView.findViewById(R.id.nome);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
// fill row data
Ambiente currentItem = getItem(position);
if(currentItem != null) {
holder.txtAmbienteNome.setText(currentItem.pegaNome());
}
return convertView;
}
public List<Ambiente> getItems() {
return mItems;
}
public void setItems(List<Ambiente> mItems) {
this.mItems = mItems;
}
class ViewHolder {
TextView txtAmbienteNome;
}
}
ListaAmbienteAcitivity (Activity)
package nermv.nermv15.activities;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import java.util.List;
import nermv.nermv15.R;
import nermv.nermv15.adapter.ListaAmbientesAdapter;
import nermv.nermv15.dao.AmbienteDAO;
import nermv.nermv15.model.Ambiente;
import nermv.nermv15.model.Projeto;
import nermv.nermv15.model.UC;
/**
* Created by norsul on 09/04/2015.
*/
public class ListaAmbientesActivity extends ActionBarActivity implements OnItemClickListener, OnClickListener {
public static final String TAG = "ListaUCActivity";
public static final int REQUEST_CODE_ADICIONAR_AMBIENTE = 60;
public static final int REQUEST_CODE_EDITAR_AMBIENTE = 61;
public static final String EXTRA_AMBIENTE_ADICIONADO = "extra_key_added_ambiente";
public static final String EXTRA_AMBIENTE_EDITADO = "extra_key_edited_ambiente";
public static final String EXTRA_AMBIENTE_CLICADO = "extra_key_clicked_ambiente";
private ListView listviewAmbientes;
private TextView emptyListAmbientes;
private ImageButton btnNovoAmbiente;
private ListaAmbientesAdapter listaAmbientessAdapter;
private List<Ambiente> listAmbienetes;
private AmbienteDAO ambienteDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_ambientes);
// initialize views
initViews();
// fill the listView
fillListView();
}
private void fillListView() {
ambienteDao = new AmbienteDAO(this);
UC clickedUC = (UC)getIntent().getSerializableExtra(ListaUCsActivity.EXTRA_UC_CLICADO);
long id_uc = clickedUC.pegaId();
listAmbienetes = ambienteDao.pegaTodosAmbientes(id_uc);
if(listAmbienetes != null && !listAmbienetes.isEmpty()) {
listaAmbientessAdapter = new ListaAmbientesAdapter(this, listAmbienetes);
listviewAmbientes.setAdapter(listaAmbientessAdapter);
}
else {
emptyListAmbientes.setVisibility(View.VISIBLE);
listviewAmbientes.setVisibility(View.GONE);
}
}
private void initViews() {
this.listviewAmbientes = (ListView) findViewById(R.id.list_ambientes);
this.emptyListAmbientes = (TextView) findViewById(R.id.empty_list_ambientes);
this.btnNovoAmbiente = (ImageButton) findViewById(R.id.btn_adicionar_ambiente);
this.listviewAmbientes.setOnItemClickListener(this);
this.btnNovoAmbiente.setOnClickListener(this);
registerForContextMenu(listviewAmbientes);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_CODE_EDITAR_AMBIENTE) {
if(resultCode == RESULT_OK) {
// add the added company to the listCompanies and refresh the listView
if(data != null) {
Ambiente editadoAmbiente = (Ambiente) data.getSerializableExtra(EXTRA_AMBIENTE_EDITADO);
if(editadoAmbiente != null) {
ambienteDao = new AmbienteDAO(this);
ambienteDao.editarAmbiente(editadoAmbiente.pegaId(), editadoAmbiente.pegaNome(), editadoAmbiente.pegaArea(), editadoAmbiente.pegaAltura(),editadoAmbiente.pegaIluminanciaPre(), editadoAmbiente.pegaIluminanciaPos());
finish();
startActivity(getIntent());
}
}
}
}
if(requestCode == REQUEST_CODE_ADICIONAR_AMBIENTE) {
if(resultCode == RESULT_OK) {
// add the added company to the listCompanies and refresh the listView
if(data != null) {
Ambiente createdAmbiente = (Ambiente) data.getSerializableExtra(EXTRA_AMBIENTE_ADICIONADO);
if(createdAmbiente != null) {
finish();
startActivity(getIntent());
}
}
}
}
else
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Selecione uma Ação");
menu.add(0, v.getId(), 0, "Editar");//groupId, itemId, order, title
menu.add(0, v.getId(), 0, "Excluir");
}
@Override
public boolean onContextItemSelected(MenuItem item){
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int index = info.position;
Ambiente clickedAmbiente = listaAmbientessAdapter.getItem(index);
if(item.getTitle()=="Editar"){
Intent intent = new Intent(this, EditarAmbienteActivity.class);
intent.putExtra(ListaAmbientesActivity.EXTRA_AMBIENTE_CLICADO, clickedAmbiente);
startActivityForResult(intent, REQUEST_CODE_EDITAR_AMBIENTE);
}
else if(item.getTitle()=="Excluir"){
showDeleteDialogConfirmation(clickedAmbiente);
}else{
return false;
}
return true;
}
private void showDeleteDialogConfirmation(final Ambiente clickedAmbiente) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Delete");
alertDialogBuilder.setMessage("Are you sure you want to delete the \""+ clickedAmbiente.pegaNome()+"\" company ?");
// set positive button YES message
alertDialogBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// delete the company and refresh the list
if(ambienteDao != null) {
ambienteDao.excluirAmbiente(clickedAmbiente);
listAmbienetes.remove(clickedAmbiente);
listaAmbientessAdapter.notifyDataSetChanged();
//refresh the listView
if(listAmbienetes.isEmpty()) {
listAmbienetes = null;
listviewAmbientes.setVisibility(View.GONE);
emptyListAmbientes.setVisibility(View.VISIBLE);
}
}
dialog.dismiss();
//Toast.makeText(ListaUCsActivity.this, R.string.company_deleted_successfully, Toast.LENGTH_SHORT).show();
}
});
// set neutral button OK
alertDialogBuilder.setNeutralButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Dismiss the dialog
dialog.dismiss();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
// show alert
alertDialog.show();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_adicionar_ambiente:
Intent intent = new Intent(this, AdicionarAmbienteActivity.class);
UC clickedUC = (UC)getIntent().getSerializableExtra(ListaUCsActivity.EXTRA_UC_CLICADO);
intent.putExtra(ListaUCsActivity.EXTRA_UC_CLICADO, clickedUC);
Projeto clickedrojeto = (Projeto)getIntent().getSerializableExtra(ListaProjetosActivity.EXTRA_PROJETO_CLICADO);
intent.putExtra(ListaProjetosActivity.EXTRA_PROJETO_CLICADO, clickedrojeto);
startActivityForResult(intent, REQUEST_CODE_ADICIONAR_AMBIENTE);
break;
default:
break;
}
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
}
The big problem here, is that the UsoPonta class is not a list, but the usoPonta values are. So how can I implement it on my actual code structure?
So inside the activity of Ambiente, when you edit a Ambiente there will be a button that load the correspondent UsoPonta for that Ambiente and them populate the list of usosPonta so the user can edit values between 0-100 for the 12 of them. I would really appreciate any help.
Aucun commentaire:
Enregistrer un commentaire