I have a ListFragment that uses a BaseAdapter and in this BaseAdapter i have a checkbox for each item of the list and i want to change the background color of the item if the you check or uncheck the checkbox. I have achieved it but i want it to save the status of the checkbox in a data base, but no matter what checkbox you click it stores the change for the last item. I think I understand why it is happening, and how to fix it but i do not know how to write the code. Here is my code:
public class TareasFragment extends ListFragment {
private String TAG = TareasFragment.class.getSimpleName();
private BaseAdapter baseAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tareas_fragment,container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
Log.d(TAG, "Actividad creada");
super.onActivityCreated(savedInstanceState);
baseAdapter = new TareasAdapter(getActivity());
setListAdapter(baseAdapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.d(TAG,"onListItemClick()");
}
}
The base Adapter:
public class TareasAdapter extends BaseAdapter {
private String TAG = TareasAdapter.class.getSimpleName();
private LayoutInflater inflador; // Crea Layouts a partir del XML
private TextView asignatura, titulo, fecha, quedan;
private CheckBox checkBox;
private SparseBooleanArray mCheckStates;
private TareasDbAdapter dbAdapter;
private Cursor cursor;
public static final String COL_ASIGNATURA = TareasDbAdapter.COL_ASIGNATURA;
public static final String COL_TITULO = TareasDbAdapter.COL_TITULO;
public static final String COL_FECHA = TareasDbAdapter.COL_FECHA;
public static final String COL_DESCRIPCION = TareasDbAdapter.COL_DESCRIPCION;
public static final String COL_HECHO = TareasDbAdapter.COL_HECHO;
private int position;
public TareasAdapter(Context contexto) {
inflador =(LayoutInflater)contexto
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
dbAdapter = new TareasDbAdapter(contexto);
}
@Override
public View getView(final int posicion, View vistaReciclada,
ViewGroup padre) {
position = posicion + 1;
Log.d(TAG,"TareasAdapter.getView()");
if (vistaReciclada == null) {
vistaReciclada= inflador.inflate(R.layout.tareas_vista_lista, null);
}
Log.d(TAG,"La posición del view es: " +position);
cursor = dbAdapter.getTarea(position);
String asign = cursor.getString(cursor.getColumnIndex(COL_ASIGNATURA));
String title = cursor.getString(cursor.getColumnIndex(COL_TITULO));
String fechaPos = cursor.getString(cursor.getColumnIndex(COL_FECHA));
int hecho = cursor.getInt(cursor.getColumnIndex(COL_HECHO));
Boolean made = hecho == 1;
Log.d(TAG,"EL VALOR DE MADE es: " +made);
cursor.close();
mCheckStates = new SparseBooleanArray(dbAdapter.size());// Cambiar con la longitud
asignatura = (TextView) vistaReciclada.findViewById(R.id.tareas_vista_lugar_asignatura_mod);
titulo = (TextView) vistaReciclada.findViewById(R.id.tareas_vista_lugar_titulo_mod);
fecha = (TextView) vistaReciclada.findViewById(R.id.tareas_vista_lugar_fecha_mod);
quedan = (TextView) vistaReciclada.findViewById(R.id.tareas_vista_lugar_quedan_mod);
checkBox = (CheckBox) vistaReciclada.findViewById(R.id.tareas_vista_lugar_checkbox);
asignatura.setText(asign);
titulo.setText(title);
fecha.setText(fechaPos);
quedan.setText("13");
checkBox.setChecked(mCheckStates.get(position, made));
vistaReciclada.setBackgroundColor(Color.parseColor("#FFFFFF"));
if (checkBox.isChecked())
vistaReciclada.setBackgroundColor(Color.parseColor("#000000"));
final View vista = vistaReciclada;
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
vista.setBackgroundColor(Color.parseColor("#000000"));
Log.d(TAG, "Vamos a poner como hecha a: " +position);
dbAdapter.update(position, 1);
} else{
vista.setBackgroundColor(Color.parseColor("#FFFFFF"));
Log.d(TAG, "Vamos a poner como NO hecha a: " + (position));
boolean a = dbAdapter.update(position, 0);
Log.d(TAG, "Se ha efectuado el cambio: " +a);
}
}
});
return vistaReciclada;
}
public boolean isChecked(int position) {
return mCheckStates.get(position + 1, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position + 1, isChecked);
}
public void toggle(int position) {
setChecked(position + 1, !isChecked(position));
}
@Override
public int getCount() {
return dbAdapter.size();
}
@Override
public Object getItem(int position) {
//A MODIFICAR
return position + 1;
}
@Override
public long getItemId(int position) {
return position + 1;
}
}
I think the problem is that the variable posicion doesn't change once the list is viewed so it has always the last value, so the solution would be to know the position in the list of the checkbox. Also onListItemClick of the TareasFragment is not working but i don't think it affects this question so i will ask this issue in another one.
Aucun commentaire:
Enregistrer un commentaire