0

I have some custom adapter for gridview in android app. When I start application first time everything is ok, but when I click button and call function refreshCalendar(), adapter "remember" text propertie (Typeface:BOLD). I check (List) calendarCells and there is no error.

public class FCalCellsAdapter extends BaseAdapter{

private static final String TAG = "FCalCellsAdapter";

private Context context;
private LayoutInflater inflater;

private List<FCalCell> calendarCells;

public FCalCellsAdapter(Context context, List<FCalCell> calendarCells){
    this.context = context;
    this.calendarCells = calendarCells;

    this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public void refreshCalendarCells(List<FCalCell> calendarCells){
    this.calendarCells = calendarCells;
}

@Override
public int getCount() {
    return calendarCells.size();
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final Holder holder;
    View v = convertView;

    if(convertView == null){
        holder = new Holder();
        v = inflater.inflate(R.layout.gridview_item_cell, null);

        holder.cellBackground = (ImageView) v.findViewById(R.id.cell_background);
        holder.cellDay = (TextView) v.findViewById(R.id.cell_day);
        holder.cellPeriodDay = (TextView) v.findViewById(R.id.cell_period_day);

        v.setTag(holder);
    }else{
        holder = (Holder) v.getTag();
    }

    if(calendarCells.get(position).getDayType() == FCalCell.DAY_TYPE_ACTIVE_MONTH){
        holder.cellBackground.setBackgroundResource(R.drawable.background_cell_white);
    }else if(calendarCells.get(position).getDayType() == FCalCell.DAY_TYPE_TODAY){
        holder.cellBackground.setBackgroundResource(R.drawable.background_cell_today);
        holder.cellDay.setTypeface(Typeface.DEFAULT_BOLD);
    }else{
        holder.cellBackground.setBackgroundResource(R.drawable.background_cell_gray);
    }

    holder.cellDay.setText(calendarCells.get(position).getNumberOfDay());
    holder.cellPeriodDay.setText(String.valueOf(calendarCells.get(position).getPeriodDayNumber()));

    return v;
}

private class Holder{
    private ImageView cellBackground;
    private TextView cellDay;
    private TextView cellPeriodDay;
}

}

refreshCalendar (main Activity)

public void refreshCalendar(){
    List<FCalCell> calendarCells = FCal.getListOfCalendarCells(actualMonth,this);
    adapter.refreshCalendarCells(calendarCells);
    adapter.notifyDataSetChanged();
}
masel.popowo
  • 1,383
  • 2
  • 8
  • 11

1 Answers1

0

You need to set the typeface of dayCell in all cases in your if else statement. The TextViews will be reused and they will remember to be bold. You will experience the same problem if your table grows to be larger than what fits on screen so it can be scrolled.

Something like this:

if(calendarCells.get(position).getDayType() == FCalCell.DAY_TYPE_ACTIVE_MONTH){
    holder.cellBackground.setBackgroundResource(R.drawable.background_cell_white);
    holder.cellDay.setTypeface(Typeface.DEFAULT);
}else if(calendarCells.get(position).getDayType() == FCalCell.DAY_TYPE_TODAY){
    holder.cellBackground.setBackgroundResource(R.drawable.background_cell_today);
    holder.cellDay.setTypeface(Typeface.DEFAULT_BOLD);
}else{
    holder.cellBackground.setBackgroundResource(R.drawable.background_cell_gray);
    holder.cellDay.setTypeface(Typeface.DEFAULT);
}
sanna
  • 1,165
  • 1
  • 16
  • 26