Skip to content

Instantly share code, notes, and snippets.

@fandrefh
Created June 26, 2015 20:00
Show Gist options
  • Save fandrefh/2382b432a2dbc479b3d5 to your computer and use it in GitHub Desktop.
Save fandrefh/2382b432a2dbc479b3d5 to your computer and use it in GitHub Desktop.
package br.com.savemoney.mastercontas;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter;
import br.com.savemoney.database.DatabaseHelper;
import br.com.savemoney.database.DatabaseHelper.Devedores;
public class ListaDevedoresActivity extends ListActivity implements OnItemClickListener, OnClickListener {
private DatabaseHelper helper;
private List<Map<String, Object>> devedores;
private AlertDialog alertDialog;
private AlertDialog alertConfirma;
private int devedorSelecionado;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
helper = new DatabaseHelper(this);
String[] from = new String[] {Devedores.NOME, Devedores.EMAIL};
int[] to = new int[] {R.id.txtNomeDevedor, R.id.txtEmailDevedor};
SimpleAdapter adapter = new SimpleAdapter(this, listarDevedores(), R.layout.lista_devedores, from, to);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
this.alertDialog = criaAlertdialog();
this.alertConfirma = criaConfirmacaoDialog();
}
private List<Map<String, Object>> listarDevedores() {
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT nome, email FROM devedor;", null);
cursor.moveToFirst();
devedores = new ArrayList<Map<String,Object>>();
for (int i = 0; i < cursor.getCount(); i++) {
Map<String, Object> devedor = new HashMap<String, Object>();
String nome = cursor.getString(0);
String email = cursor.getString(1);
devedor.put("nome", nome);
devedor.put("email", email);
devedores.add(devedor);
cursor.moveToNext();
}
cursor.close();
return devedores;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
//Guardo a posição do item na lista.
this.devedorSelecionado = position;
alertDialog.show();
//Testa se realmente capturou o id da posição do item.
Toast.makeText(this, "Devedor selecionado: " + this.devedorSelecionado, Toast.LENGTH_LONG).show();
}
@Override
public void onClick(DialogInterface dialog, int item) {
Intent intent;
String id = (String) devedores.get(devedorSelecionado).get("id");
switch (item) {
case 0:
startActivity(new Intent(this, DevedorActivity.class));
break;
case 1:// Editar Devedor
intent = new Intent(this, DevedorActivity.class);
//Adicionando/Enviando o id via conteúdo extra para outra activity
intent.putExtra("id", id);
startActivity(intent);
break;
case 2:
alertConfirma.show();
break;
case DialogInterface.BUTTON_POSITIVE:
devedores.remove(devedorSelecionado);
getListView().invalidateViews();
break;
case DialogInterface.BUTTON_NEGATIVE:
alertConfirma.dismiss();
break;
default:
break;
}
}
private AlertDialog criaAlertdialog() {
final CharSequence[] items = {
getString(R.string.novo_devedor),
getString(R.string.editar_devedor),
getString(R.string.remover_devedor)
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.acoes_devedor);
builder.setItems(items, this);
return builder.create();
}
private AlertDialog criaConfirmacaoDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.confirma_exclusao_devedor);
builder.setPositiveButton(getString(R.string.sim_exclusao_devedor), this);
builder.setNegativeButton(getString(R.string.nao_exclusao_devedor), this);
return builder.create();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment