How do we the de facto?



  • I've got a code that sets out a list of elements. I created a class. Utils in which all these instrumental functions are carried out.

    This is a function.

    /**
     * Set the font to TextView
     **/
    private void fontInit() {
        TextView tvWelcomeAct1 = (TextView) findViewById(R.id.tvWelcomAct1);
        TextView tvWelcomeAct2 = (TextView) findViewById(R.id.tvWelcomAct2);
        TextView tvWelcomeAct3 = (TextView) findViewById(R.id.tvWelcomAct3);
        TextView tvWelcomeAct4 = (TextView) findViewById(R.id.tvWelcomAct4);
        TextView tvWelcomeAct6 = (TextView) findViewById(R.id.tvWelcomAct6);
        TextView tvWelcomeAct7 = (TextView) findViewById(R.id.tvWelcomAct7);
    
    List<TextView> fontList = new ArrayList<>();
    Collections.addAll(fontList, tvWelcomeAct1, tvWelcomeAct2, tvWelcomeAct3,
            tvWelcomeAct4, tvWelcomeAct6, tvWelcomeAct7);
    
    for (int i = 0; i < fontList.size(); i++) {
        UtilClass.setFont(context, fontList.get(i));
    }
    

    }

    I'm starting to embarrass myself that I'm duplicated in all this function. Activity♪ ♪ ♪

    Ideally, I'd like to optimize this function so she can take it. context And through the comma, all vews, whatever their number. ♪ ♪

    Something like that.

    private void fontInit(context, tvWelcomeAct1, tvWelcomeAct2, tvWelcomeAct3, tvWelcomeAct4, tvWelcomeAct6, tvWelcomeAct7) {
    List<TextView> fontList = new ArrayList<>();
    Collections.addAll(fontList, только как их сюда передать);

    for (int i = 0; i &lt; fontList.size(); i++) {
        UtilClass.setFont(context, fontList.get(i));
    }
    

    Thank you for your advice.



  • Used varargs and cycle foreach:

    void fontInit(Context context, TextView ... fontList ) {
        for (TextView textView : fontlist) 
            UtilClass.setFont(context, textView);
    }
    

    We've got a method to list how much it takes. TextView after context:

    fontInit(context, tvWelcomeAct1, tvWelcomeAct2, tvWelcomeAct3);
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2