Android - Is ViewHolder pattern automatically implemented in CursorAdapter?
Asked Answered
A

2

30

I always use ViewHolder pattern in my custom ArrayAdapter classes. However, in CursorAdapter the getView() method is not mandatory required to be overridden , but has the bindView and newView methods.

My question is - does CursorAdapter re-uses views by internally implementing the ViewHolder pattern or it needs to be coded as we normally do in custom ArrayAdapter? If it needs to be coded, what is the correct way to do it?

Update

I'm using android.support.v4.widget.CursorAdapter

Avie answered 12/10, 2012 at 14:4 Comment(0)
S
40

My question is - does CursorAdapter re-uses views by internally implementing the ViewHolder pattern or it needs to be coded as we normally do in custom ArrayAdapter?

I'm not sure at what do you refer by the ViewHolder pattern. If you are referring to having a helper class to cache looking for view each time(and setting it as a tag for the row View) then the answer is no. If you want to implement this pattern you'll need to setup the holder(look for the views in the row view with findViewById) in the newView method and then set it as the tag for the row view. Then in the bindView method you can call getTag, retrieve the holder and use it. An example:

// custom CursorAdapter ...

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View rowView = ((LayoutInflater) context
                .getSystemService("layout_inflater")).inflate(
                R.layout.row_layout, parent, false);
        ViewHolder holder = new ViewHolder();
        holder.v1 = rowView.findViewById(R.id.v1);
        holder.v2 = rowView.findViewById(R.id.v2);
        rowView.setTag(holder);
        return rowView;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder holder = (ViewHolder) view.getTag();
        // use the holder filled with views
        // hlder.v1.setSomething
    }

    class ViewHolder {
        View v1, v2;
    }
// ...

If you are referring to the convertView being reused(like in non Cursor based adapters) then the answer is yes, the getView method implements this pattern, you just need to implement the newView and bindView methods and you're guaranteed to get a view which was recycled(if possible at that moment).

Stereotype answered 12/10, 2012 at 14:13 Comment(3)
@Mahendra So it was the first case. No, the holder pattern isn't implemented in CursorAdapter(from the SDK or the compatibility package).Stereotype
My query is more about 'coding the Holder in custom adapter or is it not required'? I see the CursorAdapter class also has the getView method..Avie
@Mahendra Implementing the holder is up to you. You'll get a performance improvement but it not that much as using the convertView. I've edited my answer with a small example.Stereotype
E
3

You haven't indicated whether you are using the platform version or the support library version of CursorAdapter. In either case, Android is open source, so you can look for yourself!

android.support.v4.widget.CursorAdapter
android.widget.CursorAdapter

If you're using Chrome, the Android SDK Reference Search component is super-helpful!

Essay answered 12/10, 2012 at 14:11 Comment(1)
I udpated my question... I'm using the support library versionAvie

© 2022 - 2024 — McMap. All rights reserved.