2013-03-11 69 views
0

我有一些自定義適配器的gridview在Android應用程序。當我第一次啓動應用程序時,一切正常,但是當我單擊按鈕並調用函數refreshCalendar()時,適配器「記住」文本屬性(字體:BOLD)。 我檢查(列表)calendarCells並沒有錯誤。GridView的自定義適配器記住格式化選項

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(主要活動)

public void refreshCalendar(){ 
    List<FCalCell> calendarCells = FCal.getListOfCalendarCells(actualMonth,this); 
    adapter.refreshCalendarCells(calendarCells); 
    adapter.notifyDataSetChanged(); 
} 

回答

0

你需要設置的dayCell的字體在任何情況下你如果else語句。 TextView將被重用,他們會記得大膽。如果您的表增長爲大於屏幕上顯示的大小,您可能會遇到同樣的問題,因此可以滾動。

事情是這樣的:

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); 
}