mardi 13 juin 2017

How to set OnCheckedChangeListener to a checkbox of a cell in a listview

Hi guys I am trying to set an OnCheckedChangeListener on a checkbox declared in the xml file that represent the single row of the listview (todo_item.xml), here the code of the checkbox:

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_gravity="center_vertical"
    android:text="" />

Here the part of code of the listview (fragment_bacheca.xml):

<ListView
        android:id="@+id/listView1"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

Then I use a custom adapter:`public class TaskListAdapter extends ArrayAdapter{

public TaskListAdapter(Context context, int textViewResourceId, List<TaskCell> objects){
    super(context, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) getContext()
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.item_todo, null);
    TextView task_subject = (TextView) convertView.findViewById(R.id.task_subject);
    TextView task_type = (TextView) convertView.findViewById(R.id.task_type);
    TextView task_data = (TextView) convertView.findViewById(R.id.task_data);
    CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1);

    TaskCell t = getItem(position);
    task_subject.setText(t.getMateria());
    task_type.setText(t.getTipo());
    task_data.setText(t.getData());
    if(t.getFatto().equals("1")) {
        cb.setChecked(true);
        task_subject.setPaintFlags(task_subject.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    }
    else{
        cb.setChecked(false);
    }

    return convertView;
}`

TaskCell is the class for the the data of the todo_item.

And here the code of the fragment where I call the listener:

taskView = getActivity().getLayoutInflater().inflate(R.layout.item_todo, null);
    checkbox_task = (CheckBox) taskView.findViewById(R.id.checkBox1);

    checkbox_task.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            adapter.notifyDataSetChanged();
        }
    });

    list = new LinkedList<>();
    adapter = new TaskListAdapter(getContext(), R.layout.item_todo, list);
    lv.setAdapter(adapter);

The problem is that nothing happened and with the debug i saw that the onCheckedChanged is never reached.

Anyone knows why? Thank you all




Aucun commentaire:

Enregistrer un commentaire