package Password.Principal;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import usuario.Alerts;
import usuario.ManejadorBD;
import usuario.Password;
import android.widget.CompoundButton.OnCheckedChangeListener;
public Password[] datos = null;
public Alerts alerta = new Alerts();
public AdaptadorPasswords contras = null;
public int posicion = 0,posId=0;
public ManejadorBD cnx = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.principal);
try {
datos=this.getPasswords();
if (datos == null) {
alerta.showWarning("Registrar Passwords",
"Debe registrar la información",
Principal.
this); } else {
contras = new AdaptadorPasswords(this);
}
contrasenias
= (ListView) findViewById
(R.
id.
contrasenias); contrasenias.setAdapter(contras);
ImageButton añadir = (ImageButton) findViewById(R.id.agregar);
ImageButton borrar = (ImageButton) findViewById(R.id.eliminar);
añadir.setOnClickListener(new OnClickListener() {
public void onClick
(View v
) { // TODO Auto-generated method stub
Intent envio
= new Intent
(Principal.
this, NewPassword.
class); Bundle opc=new Bundle();
opc.putString("operacion", "registrar");
envio.putExtras(opc);
setResult(RESULT_OK, envio);
startActivity(envio);
}
});
borrar.setOnClickListener(new OnClickListener() {
@Override
public void onClick
(View v
) { // TODO Auto-generated method stub
}
});
alerta.showWarning("Error en la Aplicación",
// TODO Auto-generated catch block
alerta.showWarning("Error en la Aplicación",
}
}
public void onCreateContextMenu
(ContextMenu menu,
View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menusecundario, menu);
}
@Override
public boolean onContextItemSelected
(MenuItem item
) {
@SuppressWarnings("unused")
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
switch (item.getItemId()) {
case R.id.editar:
Intent envio
= new Intent
(Principal.
this, NewPassword.
class); Bundle opc=new Bundle();
opc.putString("operacion", "editar");
opc.putInt("id", datos[posicion].getId());
envio.putExtras(opc);
setResult(RESULT_OK, envio);
startActivity(envio);
return true;
case R.id.eliminar:
Password pswd = new Password();
Toast msj2 = null;
if (pswd.
eliminarPassword(Principal.
this, datos
[posicion
].
getId())) { "El registro se ha eliminado correctamente... ", Toast.LENGTH_SHORT);
msj2.show();
datos=this.getPasswords();
if(datos.length>0 || datos!=null){
contras=new AdaptadorPasswords(this);
contrasenias.setAdapter(contras);
}else{
alerta.showWarning("Registrar Passwords",
"Debe registrar la información",
Principal.
this); }
} else {
"El registro no se ha podido eliminar correctamente.. "
+ datos[posicion].getId(), Toast.LENGTH_SHORT);
msj2.show();
}
return true;
case R.id.mostrar:
Intent envio2
= new Intent
(Principal.
this,Contras.
class); Bundle idContra=new Bundle();
idContra.putInt("Id", datos[posicion].getId());
envio2.putExtras(idContra);
setResult(RESULT_OK, envio2);
startActivity(envio2);
return true;
default:
return super.onContextItemSelected(item);
}
}
public Password[] getPasswords(){
Password[] psws=null;
cnx
= new ManejadorBD
(Principal.
this,
"passwordsBD",
1,
'r'); Cursor c
= cnx.
consultar("SELECT * FROM passwords",
null); if (c == null || c.getCount()==0)
psws=null;
else {
if (c.moveToFirst()) {
int pos = 0;
int limite = c.getCount();
psws = new Password[limite];
do {
int id = c.getInt(0);
String sitio
= c.
getString(1); String contra
= c.
getString(3); String corAlt
= c.
getString(6); psws[pos] = new Password(id, sitio, user, contra,
preg, resp, corAlt);
pos++;
} while (c.moveToNext());
}else{
psws=null;
}
}
c.close();
return (psws);
}
@SuppressWarnings("rawtypes")
class AdaptadorPasswords extends ArrayAdapter {
Activity context;
@SuppressWarnings("unchecked")
AdaptadorPasswords(Activity context) {
super(context, R.layout.lista, datos);
this.context = context;
}
public View getView
(final int position,
View convertView,
ViewGroup parent) {
final ViewHolder holder;
if (item == null) {
LayoutInflater inflater = context.getLayoutInflater();
item = inflater.inflate(R.layout.lista, null);
holder = new ViewHolder();
holder.titulo = (TextView) item.findViewById(R.id.Titulo);
holder.subtitulo = (TextView) item.findViewById(R.id.Subtitulo);
holder.activacion = (CheckBox) item
.findViewById(R.id.activacion);
holder.activacion
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(
CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
posId++;
}
else
{
posId--;
}
"el posId"+posId, Toast.LENGTH_SHORT);
msj.show();
}
});
item.setOnClickListener(new OnClickListener() {
@Override
public void onClick
(View v
) { // TODO Auto-generated method stub
Intent envio
= new Intent
(Principal.
this, Contras.
class); Bundle psw = new Bundle();
psw.putInt("Id", datos[position].getId());
envio.putExtras(psw);
setResult(RESULT_OK, envio);
startActivity(envio);
}
});
item.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick
(View v
) { // TODO Auto-generated method stub
posicion = position;
return false;
}
});
registerForContextMenu(item);
item.setTag(holder);
} else {
holder = (ViewHolder) item.getTag();
}
holder.titulo.setText(datos[position].getSitio());
holder.subtitulo.setText(datos[position].getUsuario());
return (item);
}
}
static class ViewHolder {
TextView titulo;
TextView subtitulo;
CheckBox activacion;
}
}