Im sorry if this question has been answered before but i just started programming in android studio and cant seem to find a solution.
I currently have a listview with data from an sqlite database, what ive been trying to do is click an item in the listview which should connect with another activity showing details retrieved from the sqlite database for that specific item(structured in multiple edit texts). I also would like to implement a "sign in" button so that when its pressed a data value goes down by one(and so does the edittext which contains it).
Im sorry again if its a dumb question but ive been searching for the solution all weekend and im running out of time.
Here is my database helper code
public class BaseHelper extends SQLiteOpenHelper {
String tabla="CREATE TABLE EVENTO(ID INTEGER PRIMARY KEY, NOMBRE TEXT, FECHA TEXT, HORA TEXT, CUPOS INTEGER, DESCRIPCION TEXT)";
public BaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(tabla);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS Evento");
db.execSQL(tabla);
}
}
My Mainactivity activity
public class MainActivity extends AppCompatActivity {
EditText ET_nombre, ET_fecha, ET_hora, ET_cupos, ET_descripcion;
Button bt_guardar, bt_mostrar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ET_nombre = (EditText) findViewById(R.id.ET_nombre);
ET_fecha = (EditText) findViewById(R.id.ET_fecha);
ET_hora = (EditText) findViewById(R.id.ET_hora);
ET_cupos = (EditText) findViewById(R.id.ET_cupos);
ET_descripcion = (EditText) findViewById(R.id.ET_descripcion);
bt_guardar = (Button) findViewById(R.id.bt_guardar);
bt_mostrar = (Button) findViewById(R.id.bt_mostrar);
bt_guardar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
guardar(ET_nombre.getText().toString(),ET_fecha.getText().toString(),
ET_hora.getText().toString(),Integer.parseInt(ET_cupos.getText().toString()),
ET_descripcion.getText().toString());
}
});
bt_mostrar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, agenda.class));
}
});
}
private void guardar(String Nombre, String Fecha, String Hora, int Cupos, String Descripcion) {
BaseHelper helper = new BaseHelper(this, "eventos", null, 1);
SQLiteDatabase db = helper.getWritableDatabase();
try{
ContentValues c = new ContentValues();
c.put("Nombre", Nombre);
c.put("Fecha", Fecha);
c.put("Hora", Hora);
c.put("Cupos", Cupos);
c.put("Descripcion", Descripcion);
if (ET_nombre.length() > 0 && ET_fecha.length() > 0 && ET_hora.length() > 0 && ET_cupos.length() > 0) {
db.insert("EVENTO",null,c);
db.close();
Toast.makeText(this, "Evento registrado", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "llena todas las casillas", Toast.LENGTH_LONG).show();
}
}catch (Exception e) {
Toast.makeText(this, "Error:" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
My activity which shows SQLite data in a listview
public class agenda extends AppCompatActivity {
ListView listView;
ArrayList<String> listado;
@Override
protected void onPostResume() {
super.onPostResume();
Cargar();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_agenda);
listView = (ListView) findViewById(R.id.listView);
Cargar();
if (getSupportActionBar()!=null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
private void Cargar() {
listado = Listado();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listado);
listView.setAdapter(adapter);
}
private ArrayList<String> Listado() {
ArrayList<String> datos = new ArrayList<String>();
BaseHelper helper = new BaseHelper(this, "eventos", null, 1);
SQLiteDatabase db = helper.getReadableDatabase();
String sql = "select Id, Nombre from Evento";
Cursor c = db.rawQuery(sql,null);
if(c.moveToFirst()) {
do {
String linea = c.getInt(0) +") "+ c.getString(1);
datos.add(linea);
}while(c.moveToNext());
}
db.close();
return datos;
}
}