dimanche 27 mars 2016

Android Listview checkboxes get scrambled

I am making a todo list app and I use a listview. Each item has a checkbox and a textview and has it's own Task object. The problem is when I check one box, another box 10-11 rows below it gets checked too. If you continue to scroll up and down on the list the checked checkboxes spread and and soon all of them are checked. I don't understand what I'm doing wrong. Help would be very appreciated! Thanks!

Here is my ListAdapter:

public class TaskListAdapter extends BaseAdapter {

private ArrayList<Task> tasks;
private Context context;
private TinyDB tinyDB;

public TaskListAdapter(Context context, ArrayList<Task> tasks, TinyDB tinyDB){
    this.context = context;
    this.tasks = tasks;
    this.tinyDB = tinyDB;
}
@Override
public int getCount() {
    return tasks.size();
}

@Override
public Object getItem(int position) {
    return tasks.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}


static class ViewHolder {
    AppCompatCheckBox checkBox;
    TextView taskTextView;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        convertView = layoutInflater.inflate(R.layout.list_item, null);

        holder = new ViewHolder();
        holder.checkBox = (AppCompatCheckBox) convertView.findViewById(R.id.checkBox);
        holder.taskTextView = (TextView) convertView.findViewById(R.id.taskTextView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    //checkbox
    int priority = tasks.get(position).getPriority();
    int[][] states = new int[][]{new int[]{-android.R.attr.state_checked}, new int[]{android.R.attr.state_checked}};
    int[] redColors = new int[]{context.getResources().getColor(R.color.radioBtnRed), context.getResources().getColor(R.color.radioBtnRed),};
    int[] blueColors = new int[]{context.getResources().getColor(R.color.radioBtnBlue), context.getResources().getColor(R.color.radioBtnBlue),};
    int[] greenColors = new int[]{context.getResources().getColor(R.color.radioBtnGreen), context.getResources().getColor(R.color.radioBtnGreen),};
    if(priority == 1){
        holder.checkBox.setSupportButtonTintList(new ColorStateList(states, redColors));
    }
    else if(priority == 2){
        holder.checkBox.setSupportButtonTintList(new ColorStateList(states, blueColors));
    }
    else if(priority == 3){
        holder.checkBox.setSupportButtonTintList(new ColorStateList(states, greenColors));
    }

    holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            tasks.get(position).setIsChecked(isChecked);
            tinyDB.putListObject(MainActivity.TASKS_FILE, tasks);
            MainActivity.collectCheckedTasks();
        }
    });

    boolean isChecked = tasks.get(position).isChecked();
    if(isChecked){
        holder.checkBox.setChecked(true);
    }

    //task title
    String title = tasks.get(position).getTitle();
    holder.taskTextView.setText(title);

    return convertView;
}

}




Aucun commentaire:

Enregistrer un commentaire