2

I access string resource in adapter. But I have a concern Which way to be usefull or better performance? First way seems like not usefull but does it create problems in terms of performance?

1;

Context context;
public Adapter(Context context){
     this.context = context;
}

...
...

public void onBind(Holder holder,int position) {
     holder.text.setText(context.getResources().getString(R.string.formText, position));
}

2;

Context context;
String formText;

public Adapter(Context context){
     this.context = context;
     this.formText= context.getResources().getString(R.string.formText);
}

...
...

public void onBind(Holder holder,int position) {
     holder.text.setText(String.format(formText, position));
}
kibar
  • 822
  • 3
  • 17
  • 37

1 Answers1

4

The second has less a performance hit. You avoid the lookup of the string resource more than once. Without a proper benchmark, can't say how much performance is affected.

As far as usefulness, both will work.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245