3

I have a huge listview , with more then 100,000 items. What I did now..

    PostPaid accountItem = (PostPaid)arr.get(position);
    ViewHolder holder;
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                convertView = mInflater.inflate(R.layout.list_support_item20, null);
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.accountType);
                convertView.setTag(holder);
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.text.setText(accountItem.getTitle());

What I did originally

LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_support_item, null);
TextView accountType = (TextView) convertView.findViewById(R.id.accountType);
accountType.setText(accountItem.getTitle());

I want to ask about the differences between two, and which on preferable? And how can I improve performance even more. I am testing on kitat nexus and works perfectly in both cases, but am trying to figure out gingerbread issues

Squonk
  • 48,735
  • 19
  • 103
  • 135
user3278732
  • 1,694
  • 10
  • 31
  • 67
  • 1
    View Holder type is more preferable. –  Mar 28 '14 at 08:43
  • **"I have a huge listview , with more then 100,000 items."** : Seriously? Is there no way you can break this up into categories or sub-sections. I certainly wouldn't find using a `ListView` with that many items a good experience. – Squonk Mar 28 '14 at 08:53
  • no not possible, because it is a search function. User searches for a particular value, i will return high amount of items or little amount of items. I have though specified search to be more then 2 letters. – user3278732 Mar 28 '14 at 08:55
  • But why do you have to put all items in the list if you're creating a search function. Just keep the data in a SQLite database, create a FTS3 virtual table and then do fast-text searches. Much more user-friendly. – Squonk Mar 28 '14 at 09:00

2 Answers2

2
I want to ask about the differences between two, and which on preferable?

with a large of data to represent is always preferable to use the ViewHolder pattern. As you have surely realised, if you do not use this pattern, you have to call findViewById for every view that belongs to the layout you inflated. With this pattern you look for those only once.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • what if the data is small? is it not good to use viewholder? because at times it will be little in numbers and at times large listview – user3278732 Mar 28 '14 at 08:52
  • in the worst case the benefit to implement it is little. Surely the performance will not decrease. – Blackbelt Mar 28 '14 at 08:53
1

Performance Tips for Android’s ListView

  1. use the ViewHolder pattern.

Understand how listview recycling works

How ListView's recycling mechanism works

Quoting docs

Your code might call findViewById() frequently during the scrolling of ListView, which can slow down performance. Even when the Adapter returns an inflated view for recycling, you still need to look up the elements and update them. A way around repeated use of findViewById() is to use the "view holder" design pattern.

 public View getView(int position, View convertView, ViewGroup parent) { 
         ViewHolder holder; 

         if (convertView == null) { // if convertView is null
             convertView = mInflater.inflate(R.layout.mylayout, 
                     parent, false);
             holder = new ViewHolder(); 
                 // initialize views  
            convertView.setTag(holder);  // set tag on view
        } else { 
            holder = (ViewHolder) convertView.getTag();
                    // if not null get tag 
                    // no need to initialize
        } 

        //update views here  
        return convertView; 
}

So using ViewHolder improves performance as you avoid initializing views everytime. You initialize only when view is null (if(convertView==null) {. Helps in smooth scrolling.

You missed the important part convertView.setTag(holder) and holder = (ViewHolder) ConvertView.getTag()

Android Doc of Smooth scrolling in lisview

Community
  • 1
  • 1
Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85