I have a ListView with an adapter that has a switch in the layout, so every item in the list has a switch.
I want to use this code:
mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});
But I don’t know WHERE to use it: in the adapter class or in the class containing the class, and how I can do this, knowing which element I change when I switch this switch, so I can change its properties.
Edit: This is an adapter class with an implemented method. It starts every time the getView method is called, and not when it should start (when the switch is switched), and the logs are displayed in the logarithm, but the object that switches to switch mode does not change, when I scroll down and scroll up, the switch returns in the initial state :
package com.nahue.actions;
import java.util.ArrayList;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
public class AdapterActions extends ArrayAdapter<Action>{
static class ViewHolderItem {
TextView codigo;
TextView accion;
TextView evento;
Switch UnSwitch;
}
Activity context;
ArrayList<Action> listaActions;
public AdapterActions(Activity context, ArrayList<Action> listaActions) {
super(context, R.layout.layout_adapter_actions, listaActions);
this.context = context;
this.listaActions = listaActions;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolderItem viewHolder;
viewHolder = new ViewHolderItem();
if(convertView==null){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.layout_adapter_actions, parent, false);
viewHolder = new ViewHolderItem();
viewHolder.codigo = (TextView) convertView.findViewById(R.id.codigo);
viewHolder.accion = (TextView) convertView.findViewById(R.id.accion);
viewHolder.evento = (TextView) convertView.findViewById(R.id.evento);
viewHolder.UnSwitch = (Switch) convertView.findViewById(R.id.activa);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolderItem) convertView.getTag();
}
Action a = new Action(0, 1, 1, true);
Accion LaAccion = new Accion(0, "", "");
Evento ElEvento = new Evento(0, "", "");
ActionsSQLite base = new ActionsSQLite(context, "Actions", null,1);
SQLiteDatabase db1 = base.getReadableDatabase();
db1 = context.openOrCreateDatabase("Actions",SQLiteDatabase.OPEN_READONLY, null);
String query = "SELECT * FROM Actions WHERE Id = " + String.valueOf(position + 1);
Cursor c1 = db1.rawQuery(query, null);
try{
if(c1!=null){
int i = c1.getColumnIndexOrThrow("Id");
int j = c1.getColumnIndexOrThrow("IdAccion");
int k = c1.getColumnIndexOrThrow("IdEvento");
int l = c1.getColumnIndexOrThrow("Activa");
boolean esActiva;
while(c1.moveToNext()){
if (c1.getInt(l) == 0){
esActiva = false;
} else
{
esActiva = true;
}
a = new Action(c1.getInt(i), c1.getInt(j), c1.getInt(k), esActiva);
}
}
else
Toast.makeText(context.getApplicationContext(),
"No hay nada :(", Toast.LENGTH_LONG).show();
}
catch (Exception e){
Log.i("bdActions", "Error al abrir o crear la base de datos" + e);
}
if(db1!=null){
db1.close();
}
EventosSQLite base2 = new EventosSQLite(this.context, "Eventos", null, 1);
SQLiteDatabase db2 = base2.getReadableDatabase();
db2 = context.openOrCreateDatabase("Eventos",SQLiteDatabase.OPEN_READONLY, null);
String query2 = "SELECT * FROM Eventos WHERE Id = " + a.getIdEvento();
Cursor c2 = db2.rawQuery(query2, null);
try{
if(c2!=null){
int h = c2.getColumnIndexOrThrow("Id");
int i = c2.getColumnIndexOrThrow("Nombre");
int j = c2.getColumnIndexOrThrow("Descripcion");
while(c2.moveToNext()){
ElEvento = new Evento(c2.getInt(h), c2.getString(i), c2.getString(j));
}
}
else
Toast.makeText(context.getApplicationContext(),
"No hay nada :(", Toast.LENGTH_LONG).show();
}
catch (Exception e){
Log.i("bdEventos", "Error al abrir o crear la base de datos" + e);
}
if(db2!=null){
db2.close();
}
AccionesSQLite base3 = new AccionesSQLite(context, "acciones", null,1);
SQLiteDatabase db3 = base3.getReadableDatabase();
db3 = context.openOrCreateDatabase("Acciones", SQLiteDatabase.OPEN_READONLY, null);
String query3 = "SELECT * FROM Acciones WHERE Id = " + String.valueOf(a.getIdAccion());
Cursor c3 = db3.rawQuery(query3, null);
try{
if(c3!=null){
int h = c3.getColumnIndexOrThrow("Id");
int i = c3.getColumnIndexOrThrow("Nombre");
int j = c3.getColumnIndexOrThrow("Descripcion");
while(c3.moveToNext()){
LaAccion = new Accion(c3.getInt(h), c3.getString(i), c3.getString(j));
}
}
else
Toast.makeText(context.getApplicationContext(),
"No hay nada :(", Toast.LENGTH_LONG).show();
}
catch (Exception e){
Log.i("bdAcciones", "Error al abrir o crear la base de datos" + e);
}
if(db3!=null){
db3.close();
}
viewHolder.UnSwitch.setChecked(a.getActiva());
if(a != null) {
viewHolder.codigo.setText(String.valueOf(a.getId()));
viewHolder.codigo.setTag(a.getId());
viewHolder.accion.setText(LaAccion.getNombre());
viewHolder.evento.setText(ElEvento.getNombre());
viewHolder.UnSwitch.setChecked(a.getActiva());
}
viewHolder.UnSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.e("Listener del switch", "Entró");
listaActions.get(position).setActiva(isChecked);
Log.e("Listener del switch", "Modificó la action en la lista del adapter");
ActividadPrincipal.listaActions.get(position).setActiva(isChecked);
Log.e("Listener del switch", "Modificó la action en la lista de la ActividadPrincipal");
int isActiva;
if(isChecked == true)
{
isActiva = 1;
Log.e("Listener del switch", "isActiva = 1");
} else
{
isActiva = 0;
Log.e("Listener del switch", "isActiva = 0");
}
String query = "UPDATE Actions SET Activa = " + String.valueOf(isActiva) + " WHERE Id = " + String.valueOf(position);
Log.e("Listener del switch", "query creado");
ActionsSQLite helper1 = new ActionsSQLite(context, "Actions", null, 1);
Log.e("Listener del switch", "Creo el helper");
SQLiteDatabase db = helper1.getWritableDatabase();
Log.e("Listener del switch", "obtenida la base escribible");
db.execSQL(query);
Log.e("Listener del switch", "Query ejecutado");
}});
return convertView;
}
}