Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8345653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:45:00+00:00 2026-06-09T06:45:00+00:00

This is a simple implementation of the ViewHolder pattern. I have a cursor containing

  • 0

This is a simple implementation of the ViewHolder pattern. I have a cursor containing articles and I want to keep the title in the row tag. At this stage I (try to) keep the actual string and the corresponding textview in the row tag plus the actual string in the textview’ s tag.

class ArticleListCursorAdapter extends SimpleCursorAdapter {
    class ViewHolder {
        String strTitle = null;
        View viewTitle = null;

        ViewHolder(View base, String strTitle) {
            this.viewTitle = base.findViewById(R.id.textTitle);
            this.strTitle = strTitle;
        }

        @Override
        public String toString() {
            return new StringBuffer()
                .append("strTtitle [").append(strTitle).append("] ")
                .append("viewTitle [").append(((TextView)viewTitle).getText()).append("] ")
                .append("viewTitle title (tag) [").append(viewTitle.getTag()).append("] ")
                .toString();
        }
    }

    public ArticleListCursorAdapter(Context context, int layout, Cursor c, String[] from,
            int[] to, int flags) {
        super(context, layout, c, from, to, flags);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = super.getView(position, convertView, parent);
        ViewHolder holder = null;
        // if (convertView != null)
        holder = (ViewHolder)row.getTag();

        Log.v(TAG, "getView for [" + position + "]. holder ["
                    + (holder == null ? "null holder" : holder.toString()) + "]");
        Log.v(TAG, "getView other info: rendering view title ["
                            + ((TextView)row.findViewById(R.id.textTitle)).getText()
                                    .toString() + "]");

        if (holder == null) {
            Cursor cursor = (Cursor)ArticleListCursorAdapter.this.getItem(position);
            final String strTitle = cursor.getString(cursor.getColumnIndex(ArticleData.C_TITLE));
            holder = new ViewHolder(row, strTitle);
            Log.v(TAG, "getView setup onclick for pos [" + position + "] and title ["+ strTitle + "]");
            holder.viewTitle.setTag(strTitle);
            row.setTag(holder);
        }

        return row;
    }
}

When I run it I expect to receive a bunch of:

getView for [x]. holder [strTitle [7777777] viewTitle [7777777] viewTitle title (tag)  [7777777] ]
getView other info: rendering view title [7777777]

if the article title is 7777777. What I actually receive is a weird mix of values

(4 items visible at a time, first page)
getView for [0]. holder [null holder]
getView other info: rendering view title [1111111]
getView setup onclick for pos [0] and title [1111111]
getView for [1]. holder [null holder]
getView other info: rendering view title [2222222]
getView setup onclick for pos [1] and title [2222222]
getView for [2]. holder [null holder]
getView other info: rendering view title [3333333]
getView setup onclick for pos [2] and title [3333333]
getView for [3]. holder [null holder]
getView other info: rendering view title [4444444]
getView setup onclick for pos [3] and title [4444444]

(page down)

getView for [4]. holder [strTitle [1111111] viewTitle [5555555] viewTitle title (tag)  [1111111] ]
getView other info: rendering view title [5555555]
getView for [5]. holder [null holder]
getView other info: rendering view title [6666666]
getView setup onclick for pos [5] and title [6666666]

one more down

getView for [6]. holder [strTitle [2222222] viewTitle [7777777] viewTitle title (tag)  [2222222] ]
getView other info: rendering view title [7777777]
getView for [7]. holder [strTitle [3333333] viewTitle [8888888] viewTitle title (tag)  [3333333] ]
getView other info: rendering view title [8888888]
getView for [8]. holder [strTitle [4444444] viewTitle [9999999] viewTitle title (tag)  [4444444] ]
getView other info: rendering view title [9999999]

(scroll up)

getView for [4]. holder [strTitle [4444444] viewTitle [5555555] viewTitle title (tag)  [4444444] ]
getView other info: rendering view title [5555555]
getView for [3]. holder [strTitle [3333333] viewTitle [4444444] viewTitle title (tag)  [3333333] ]
getView other info: rendering view title [4444444]

(scroll up again)

getView for [2]. holder [strTitle [1111111] viewTitle [3333333] viewTitle title (tag)  [1111111] ]
getView other info: rendering view title [3333333]
getView for [1]. holder [strTitle [2222222] viewTitle [2222222] viewTitle title (tag)  [2222222] ]
getView other info: rendering view title [2222222]
getView for [0]. holder [strTitle [6666666] viewTitle [1111111] viewTitle title (tag)  [6666666] ]
getView other info: rendering view title [1111111]

1/ Why the string part of the ViewHolder keeps changing?
2/ How can the viewTitle TextView alters its tag contents (the 5555555 viewTitle above has 1111111 in it’s tag one time and 444444 the other)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:45:02+00:00Added an answer on June 9, 2026 at 6:45 am

    Adapters recycle views to save resources. Imagine a ListView with 1,000 rows: it would be slow and unnecessary to create a unique View for each row. So the adapter creates only enough Views that are visible, plus a few more for scrolling; and simply reuses these recyclable rows over and over.

    What you are seeing is that you have set one ViewHolder to each recyclable row (0-3) but when you scroll the TextViews change to reflect the list data. This is why recyclable row #0 has the strTitle value for row #0 but the TextView for the actual row #4.

    Addition from Comments

    Theoretically I could make 1,000 [ViewHolders] for my 1,000 rows

    Yes you could, but just like having 1000 unique rows, this is excessive. I want to propose a more efficient way to do what you want:

    class ArticleListCursorAdapter extends SimpleCursorAdapter {
        private int cTitleIndex;
    
        public ArticleListCursorAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
            super(context, layout, cursor, from, to, 0);
    
            // Rather than check for the column index of C_TITLE every time 
            //   you use getView(), we check it only once. 
            cTitleIndex = cursor.getColumnIndex(ArticleData.C_TITLE);
        }
    
        // This method binds the Cursor data to your row
        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            super.bindView(view, context, cursor);
    
            ViewHolder holder = (ViewHolder) view.getTag();
            // Create a new ViewHolder for each recyclable row
            if(holder == null) {
                holder = new ViewHolder();
                holder.viewTitle = (TextView) view.findViewById(R.id.textTitle);
                view.setTag(holder);
            }
    
            // Update this value each time the row is recycled, we do this in 
            //   bindView() because we already have access to the Cursor
            holder.strTitle = cursor.getString(cTitleIndex);
    
            // I'm not sure what this does but I kept it
            holder.viewTitle.setTag(holder.strTitle);
        }
    
        public class ViewHolder {
            String strTitle;
            // Changed viewTitle to a TextView to unnecessary conversions in this example
            TextView viewTitle; 
        }
    }
    

    I moved everything from getView() to bindView(). In a CursorAdapter the getView() method calls bindView(), this is where all of the data relevant to the Cursor happens. Since most of your code is Cursor related moving the code here saves the extra work of finding the Cursor in getView(). You can still override getView() and call on the ViewHolder, but in this case it is no longer necessary.

    I also pulled out the search for the column index to stop the extra work there. (Understand this isn’t perfect, calling a method like adapter.changeCursor() might rearrange the column indices so you would need to re-check the index. But this is a basic example, I didn’t want to over think this.)

    Now if you add your LogCat statements back in, you’ll notice that as you scroll up and down the row will retain matching 111111 with 111111 and 444444 with 444444 pairs. Hope that clears some things up!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple html multiline tooltip implementation: this.tooltip = function(tag) { xOffset =
I have made a simple implementation of INotifyDataErrorInfo in a WPF 4.5 project. This
I was writing an answer to this question when noticed that my simple implementation
In this simple example, i want to create a String array populated with each
We have in place a simple implementation of GA and have had for some
I have a very simple AsyncTask implementation example and am having problem in testing
We have an XML file with a very simple implementation of XLink : <root
I tried a very simple implementation, like this: @implementation ScrollingTextField - (void)scrollWheel:(NSEvent *)event {
Suppose I have this simple contract which I've taken from MS examples and altered
im tryin to use this simple function: http://code.google.com/p/canvasimagegradient/ i have to create a linear

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.