dimanche 20 août 2017

Android: Checkbox in listview (how to create OnCheckedChangeListener in Adapter)

I'm creating a To-do list application and I have a question regarding to using checkboxes and its listeners in List Adapter. My single row in listview contains three TextViews and one Checkbox. I want to change background of single row when user "check" the checkbox. I have read that i should put checkbox listener in my adapter class and so I did it. Now is the problem - when i add few rows to my listview and left the checkbox unchecked for all of them all works fine, but when I add a row, check the checkbox and try to add another one I get error

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setBackgroundColor(int)' on a null object reference

Below is code of my adapter. Thank you for any advice. I'm just starting with Android programming so thank you for understanding in advance.

public class ToDoAdapter extends ArrayAdapter<ToDoTask> {


ArrayList<ToDoTask> objects;
Context context;
int resource;

public ToDoAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull ArrayList<ToDoTask> objects) {
    super(context, resource, objects);
    this.objects = objects;
    this.context = context;
    this.resource = resource;
}

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
    View view = convertView;
    ToDoHolder toDoHolder = null;

    if (view == null) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.row, parent, false);

        toDoHolder = new ToDoHolder();
        toDoHolder.rowTitle = (TextView) view.findViewById(R.id.rowTitle);
        toDoHolder.rowDesc = (TextView) view.findViewById(R.id.rowDesc);
        toDoHolder.rowDate = (TextView) view.findViewById(R.id.rowDate);
        toDoHolder.rowIsDone = (CheckBox) view.findViewById(R.id.rowCheckBoxDone);

        toDoHolder.rowIsDone.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
                if(checked){
                    parent.getChildAt(position).setBackgroundColor(Color.parseColor("#8FE370"));
                }
                else
                    parent.getChildAt(position).setBackgroundColor(Color.WHITE);
            }
        });

        view.setTag(toDoHolder);
    } else {
        toDoHolder = (ToDoHolder) view.getTag();
    }

    ToDoTask object = objects.get(position);
    toDoHolder.rowTitle.setText(object.getTitle());
    toDoHolder.rowDesc.setText(object.getDescription());
    toDoHolder.rowDate.setText(object.getDate());
    toDoHolder.rowIsDone.setChecked(object.getDone());

    return view;
}

static class ToDoHolder {
    TextView rowTitle;
    TextView rowDesc;
    TextView rowDate;
    CheckBox rowIsDone;
}
}

enter image description here




Aucun commentaire:

Enregistrer un commentaire