14

I am working on reuse of cell and currently i am working in getview method of adapter and i got one problem that my getview method called 9 times but it should call only 5 times at the starting of app because i am displaying only 5 rows.

public View getView(int position, View view, ViewGroup viewgroup) 
{   
    ViewHolder holder=null;

    if(view==null)
      {  
               view = inflater.inflate(R.layout.my_row_layout, viewgroup, false);
              Log.d("Testing","View is nuLL");
                holder.thumbnils  = (ImageView)view.findViewById(R.id.Thumbnils);
                     holder.hover =(ImageButton)view.findViewById(R.id.hover);
                    holder.title  =(TextView)view.findViewById(R.id.title);
                    holder.hoveroverlay  =(ImageView)view.findViewById(R.id.hoveroverlay);
               }view.setTag(holder);
             }
         else
    {
        Log.d("Testing","view is not null");

        holder = (ViewHolder) view.getTag();
       }

          return view;
      }

          @Override
public int getCount() {
    // TODO Auto-generated method stub

    if(data.size()%NumberOfCell==0)
    {
        //return Number rows in listview 
        return data.size() /NumberOfCell;

    }
    else
    {
        //If total number of videos are ODD than display one row extra for remaining videos
        return data.size() /NumberOfCell+1;
    }

}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 1;
}

    Here is my xml

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/imageView2"
    android:layout_alignLeft="@+id/linearLayout1"
    android:layout_below="@+id/linearLayout1"
    android:scrollbars="none"
     android:divider="#000000" 
      android:dividerHeight="2dp"
     >
</ListView>
  </RelativeLayout>

Please help me guys Thank you.........

2 Answers2

63

Make your listview height equal to fill_parent and try it again..

Swap-IOS-Android
  • 4,363
  • 6
  • 49
  • 77
  • 7
    @NSArray As Romain Guy Suggested in his answer This is not an issue, there is absolutely no guarantee on the order in which getView() will be called nor how many times. In your particular case you are doing the worst thing possible with a ListView by giving it a height=wrap_content. This forces ListView to measure a few children out of the adapter at layout time, to know how big it should be. This is what provides ListView with the convertViews you see passed to getView() even before you scroll. – Swap-IOS-Android Aug 22 '13 at 09:54
  • @NSArray refer my post for more details http://stackoverflow.com/a/14108676/1939564 – Muhammad Babar Aug 23 '13 at 08:21
  • This little information, made my application 3 or 4 times fast. OMG! THANKS SO MUCH – alicanbatur Apr 16 '14 at 07:56
  • Thanks--Another Android glitch!! – Ash Oct 13 '15 at 13:49
  • You saved my ass! this bit of information saved my day and now I can race for app release. Thanks for it – Ashwin Oct 17 '15 at 07:37
  • really it was unexpected great answer. Thank you much – Pavya Feb 26 '16 at 13:16
  • Just brilliant, very insightful- I had no clue what was slowing down my lists. – jai Mar 11 '16 at 06:53
  • great answer. Thanks much! – user2234 May 03 '16 at 06:11
0

Try like this...

public View getView(int position, View convertView, ViewGroup viewgroup) 
{   
View view = convertView;
    ViewHolder holder=null;

    if(convertView==null)
      {  
               view = inflater.inflate(R.layout.my_row_layout, viewgroup, false);
              Log.d("Testing","View is nuLL");
                holder.thumbnils  = (ImageView)view.findViewById(R.id.Thumbnils);
                     holder.hover =(ImageButton)view.findViewById(R.id.hover);
                    holder.title  =(TextView)view.findViewById(R.id.title);
                    holder.hoveroverlay  =(ImageView)view.findViewById(R.id.hoveroverlay);
               view.setTag(holder);
             }
         else
    {
        Log.d("Testing","view is not null");

        holder = (ViewHolder) view.getTag();
       }
}
Hariharan
  • 24,741
  • 6
  • 50
  • 54