input
input

Reputation: 7519

Weird behavior with CustomAdapter and CustomTextView

I have a custom TextView which I'm using in a custom GridView adapter. The custom TextView is using a custom font for translation purposes. This works fairly well in devices which have the locale installed by default.

However, in devices, in which the language is not installed, it is displaying a strange behavior. When the app loads the first time, the TextViews don't display with the custom font. However when I press the refresh button to reload the fragment, the TextViews display with the custom font.

I'm not sure why this is happening.

This is happening with all the custom Adapters in my application in which I'm using the custom TextView.

Pretty basic adapter:

public class CalendarWeekAdapter extends BaseAdapter{

    private String[] weekdays;
    Context mContext;
    private LayoutInflater mInflater;

       public CalendarWeekAdapter(Context context, int firstDay)
       {
              mContext=context;
              mInflater = LayoutInflater.from(context);
              weekdays = context.getResources().getStringArray(R.array.weekdays); 
       }
       public int getCount()
       {
              return weekdays.length;
       }
       public Object getItem(int position)
       {
              return position;
       }
       public long getItemId(int position)
       {
              return position;
       }
       public View getView(int position, View convertView, ViewGroup parent)
       {
              ViewHolder holder=null;
              if(convertView==null)
              {
                     convertView = mInflater.inflate(R.layout.calendar_week, parent,false);
                     holder = new ViewHolder();
                     holder.txtWeekdays=(CustomTextView)convertView.findViewById(R.id.weekdays);
                     if(position==0)
                     {                             
                           convertView.setTag(holder);
                     }
              }
              else
              {
                     holder = (ViewHolder) convertView.getTag();
              }


             holder.txtWeekdays.setText(weekdays[position]);


             return convertView;
       }

}
class ViewHolder
{        
          CustomTextView txtWeekdays;                 
}

Basic CustomTextView:

public class CustomTextView extends TextView {
        public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }

        public CustomTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }

        public CustomTextView(Context context) {
            super(context);
            init();
        }

        private void init() {
            if (!isInEditMode()) {
                setTypeface(Utils.getFont(getContext()));
            }
        }
}

Upvotes: 0

Views: 56

Answers (1)

Streets Of Boston
Streets Of Boston

Reputation: 12596

This is may not be the answer, but i don't understand this if-statement:

                 holder.txtWeekdays=(CustomTextView)convertView.findViewById(R.id.weekdays);
                 if(position==0)
                 {                             
                       convertView.setTag(holder);
                 }

Why is it there? All your newly inflated convertViews should have holder as their tag. Just remove the 'if' around the convertView.setTag(holder):

          if(convertView==null)
          {
                 convertView = mInflater.inflate(R.layout.calendar_week, null,false);
                 holder = new ViewHolder();
                 holder.txtWeekdays=(CustomTextView)convertView.findViewById(R.id.weekdays);
                 convertView.setTag(holder);
          }
          ...

and see if this will improve or even fix your situation.

Upvotes: 1

Related Questions