dimanche 20 décembre 2015

CheckBox in ListFragment with BaseAdapter with DataBase

I have a BaseAdapter with some items and a checkBox, i want to change the color of the view when its checkBox changes. I have achieved it but the problem is that it doesn't work until you press a view, if you click a checkBox without having clicked a view before it doesn't work because the metod onListItemClick isn't call.My code:

public class TareasFragment extends ListFragment {
private String TAG = TareasFragment.class.getSimpleName();
private BaseAdapter baseAdapter;
private TareasDbAdapter dbAdapter;
@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);
    dbAdapter = new TareasDbAdapter(getActivity());
    ListView l = (ListView) getActivity().findViewById(android.R.id.list);
}

@Override
public void onListItemClick(ListView l, final View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    final int posicion = position + 1;
    Log.d(TAG,"onListItemClick()");
    CheckBox checkBox = (CheckBox) v.findViewById(R.id.tareas_vista_lugar_checkbox);
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                v.setBackgroundColor(Color.parseColor("#000000"));
                dbAdapter.update(posicion, 1);
            } else {
                v.setBackgroundColor(Color.parseColor("#FFFFFF"));
                dbAdapter.update(posicion, 0);
            }
        }
    });
}

}

I have also tried to manage it in my class TareasAdapter but i could realise how to save the state of the checkBox in my data base.




Aucun commentaire:

Enregistrer un commentaire