mercredi 26 août 2015

Custom ListView with checkbox scrolling makes all rows same

Only working component of this view when i scroll is checkboxes because there is a seperate tag for each checkbox. Text also works but strike through does not its property applies all after scrolling when it goes below the screen(which is wrong) and same with drawCalendar function which is a view((which is wrong too, it draws other's rows images ). Do i need to set tag for each component? If so how do i do that?

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    ViewHolder holder;
    LayoutInflater inflater;
    inflater = (LayoutInflater)context.getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {

        convertView = inflater.inflate(R.layout.tasks_row,null);

        // Create a ViewHolder and store references to the two children views
        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.taskTv);
        holder.calendar =(LinearLayout) convertView.findViewById(R.id.calendar);
        holder.cb = (CheckBox) convertView.findViewById(R.id.taskCb);

        // The tag can be any Object, this just happens to be the ViewHolder
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }


    final Task t = this.childData.get(childPosition);

    lis = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            checked[(int)buttonView.getTag()] = isChecked;
            db = new TaskDb(context);
            if(isChecked)
                t.setStatus(DefaultValues.TASK_STATUS_COMPLETED);
            else
                t.setStatus(DefaultValues.TASK_STATUS_NOTDONE);
            db.updateTask(t);
        }
    };

    String text = t.getText();
    holder.text.setText(text);
    int status = t.getStatus();

    long curTime = new Date().getTime();
    if(t.getRemindDate() < curTime && status == DefaultValues.TASK_STATUS_NOTDONE)
        t.setStatus(DefaultValues.TASK_STATUS_OVER_NOTDONE);
    else if(t.getRemindDate() < curTime && status == DefaultValues.TASK_STATUS_COMPLETED)
        t.setStatus(DefaultValues.TASK_STATUS_OVER_DONE);

    status = t.getStatus();
    holder.cb.setTag(childPosition);
    holder.cb.setOnCheckedChangeListener(null); // listener must be removed so db update does not occur
    holder.cb.setChecked(checked[childPosition]);
    holder.cb.setOnCheckedChangeListener(lis);
    if(status == DefaultValues.TASK_STATUS_OVER_DONE || status == DefaultValues.TASK_STATUS_OVER_NOTDONE) {
        holder.text.setPaintFlags(holder.text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    }

    DateConverter dc = new DateConverter(context,t.getRemindDate());
    String[] dateStr = dc.getTaskDateFormat();
    holder.calendar.addView(new DrawCalendar(context, dateStr));//drawCalendar(dateStr[0],dateStr[1]));
    holder.calendar.invalidate();


    return convertView;




Aucun commentaire:

Enregistrer un commentaire