0

I have in my code two types for Adapter implementations and I'm not sure which one is better.

Here is my updater pattern:

public View getView(int position, View convertView, ViewGroup parent) {
    Updater content;
    if(convertView == null) {
        final TextView msg = new TextView(parent.getContext());
        content = new Updater() {
            public void update(...) {
                // code to update the control msg
            }
        };
        convertView = msg;
        convertView.setTag(content);
    } else {
        content = (Updater)convertView.getTag();
    }
    content.update(...);
    return convertView;
}

// ...

private interface Updater {
    void update(...);
}

And here is my ViewHolder pattern:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if(convertView == null) {
        convertView = inflater.inflate(...);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder)convertView.getTag();
    }
    holder.setData(...);
    return convertView;
}

// ...

private class ViewHolder {
    public final TextView text;

    public ViewHolder(final View owner) {
        text = (TextView)owner.findViewById(R.id.text);
    }

    public void setData(...) {
        // ...
    }
}

I'm wondering which one is more effency? I could also implement the Updater interface in a more complex control, so it is simpler to update differnt controls in a common way. So I could just cast the convertView to my interface without using getTag(). But in the code above I use a final variable which is defined outside of my interface implementation. Can somebody tell me if this can make performance problems? I don't know what happens internal in java with a final variable.

If my updater pattern is bad please tell me, why I should use the ViewHolder pattern except that this pattern is from Google IO.

rekire
  • 47,260
  • 30
  • 167
  • 264
  • 2
    i don't understand the point of having an interface for the updater, as apparently you have only one implementation. therefore, the difference seems to lie solely in the place where you put the update/setData implementation, that is either in an anonymous class in the getView or in the ViewHolder class. To me it is quite the same – njzk2 Apr 18 '13 at 10:11
  • My point is that I'm not sure if I should use multiple final fields outside of the interface implementation. Is this inefficient? I don't know what the final keyword does in the Java VM or the Dalvik VM. The code of the both *update* functions are almost equal. I just could have multiple elements which needs to be updated. – rekire Apr 18 '13 at 10:32

1 Answers1

0

Decisions like this have almost no effect on performance. Both solutions are equally efficient. The final keyboard may actually increase performance by allowing the compiler to make certain optimizations, but they will be certainly unoticeable.

Dylan P
  • 150
  • 1
  • 9
  • My point is that when my adapter needs to handle multiple types (`getViewTypeCount()`) and I have 1000 elements and the user scrolls fast thrue this list my app needs to perform the updates fast. So this matters. But in general you are right. I found [this answer](http://stackoverflow.com/a/4732617/995926) which supports your statment that there are no bad effects. – rekire Apr 19 '13 at 05:43