package com.example.quizsqlite;
import android.support.v7.app.ActionBarActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnClickListener {
EditText identificacion,nombre;
Spinner voto;
TextView resultado;
Button botonInsertar,botonConsultar;
SQLiteDatabase db;
String [] datos ={"voto","si","no"};
int contadorSi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nombre = (EditText)findViewById(R.id.etNombre);
voto = (Spinner)findViewById(R.id.spVoto);
resultado = (TextView)findViewById(R.id.tvResultado);
botonInsertar = (Button)findViewById(R.id.btnInsert);
botonInsertar.setOnClickListener(this);
botonConsultar = (Button)findViewById(R.id.btnConsultar);
botonConsultar.setOnClickListener(this);
DbHelper Helper = new DbHelper(this,"DbVotaciones", null, 1);
db = Helper.getWritableDatabase();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.itemlistavoto,datos);
voto.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
if (v == botonInsertar) {
int iden = Integer.parseInt(identificacion.getText().toString());
String nom = nombre.getText().toString();
String respuestaVoto = voto.getSelectedItem().toString();
ContentValues valores = new ContentValues();
valores.put("identificacion", iden);
valores.put("nombre", nom);
valores.put("voto", respuestaVoto);
db.insert("TbPersonas", null, valores);
identificacion.setText("");
nombre.setText("");
Toast.makeText(this, "se ha insertado correctamente", Toast.LENGTH_SHORT).show();
}else
if (v == botonConsultar) {
Cursor C = db.rawQuery("select voto from TbPersonas", null);
if (C.moveToFirst()) {
do {
String votoTemp = C.getString(C.getColumnIndex("voto"));
if (votoTemp == "si") {
contadorSi = contadorSi + 1;
}
} while (C.moveToNext());
resultado.setText(contadorSi + "personas votaron que si");
}else
resultado.setText("ninguna persona voto que si");
}
}
}