kibAr kibAr - 1 month ago 13
Android Question

Android getString performance issue in adapter holder

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

Answer

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.