1

The getView() method in my CustomAdapter is called every time I scroll up or down.

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View vi = convertView;
    ViewHolder holder;
    if (convertView == null) {
        vi = inflater.inflate(R.layout.projectlist, null);

        holder = new ViewHolder();
        holder.projectTitelTextView = (TextView) vi.findViewById(R.id.projectTitle);
        holder.projectInfoTextView = (TextView) vi.findViewById(R.id.projectInfo);
        holder.projectImageImageView = (ImageView) vi.findViewById(R.id.projectImage);
        holder.projectDeadline = (TextView) vi.findViewById(R.id.projectdeadline);

        vi.setTag(holder);
    } else {
        holder = (ViewHolder) vi.getTag();
    }
    if (projectItems.size() <= 0) {
        [...]
    } else {
         [...]
        }
    }
    return vi;
}

The ListView XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity" tools:ignore="MergeRootFrame">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <ListView android:id="@+id/android:list"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:focusable="false"
        android:clickable="false"
        android:smoothScrollbar="true" />

</LinearLayout>

Why it is called every time I scroll up or down? Assuming, I have 10 Items displayed. If I scroll down, the getView() called one time and it show me one more Item. If I hard scroll and want to go to the end of the list. It get called multiple times until the data is loaded and displayed.

If I want to scroll then up, the same scenario happens.

What can I do to prevent calling multiple times getView() or is it necessary?

Kind Regards

korunos
  • 768
  • 3
  • 11
  • 31
  • 1
    It's normal behaviour of ListView adapter to call getView() method multiple times – Rajesh Jadav Aug 31 '15 at 07:44
  • check this http://stackoverflow.com/questions/11648995/view-is-getting-inflated-everytime-in-getview-findviewbyid-is-done-many-ti if it helps – Raghunandan Aug 31 '15 at 07:45
  • thank you! I didn't know that! – korunos Aug 31 '15 at 07:48
  • on scroll ListView, for every item, getView() is called which returns a view for your ListView Item. Its the default feature which is awesome but have to maintain values based on position of item in ListView get in getView() method whenever you scroll. – Androider Aug 31 '15 at 07:56

3 Answers3

4

This is how ListAdapters work. getView() is used whenever a new list item needs to be created, or at least when a list item needs to be updated with data for a particular position.

ataulm
  • 15,195
  • 7
  • 50
  • 92
0

As Suggested by RomainGuy

This is not an issue, there is absolutely no guarantee on the order in which getView() will be called nor how many times.

It's normal behaviour of ListView adapter to call getView() method multiple times.

Community
  • 1
  • 1
Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
-1

If it is not necessaire to change something if you scroll you coud try something like that.

boolean fisttime = true;

@Override public View getView(int position, View convertView, ViewGroup parent) {

if(position == 0 && isFirstRun) {
    View vi = convertView;
ViewHolder holder;
if (convertView == null) {
    vi = inflater.inflate(R.layout.projectlist, null);

    holder = new ViewHolder();
    holder.projectTitelTextView = (TextView) vi.findViewById(R.id.projectTitle);
    holder.projectInfoTextView = (TextView) vi.findViewById(R.id.projectInfo);
    holder.projectImageImageView = (ImageView) vi.findViewById(R.id.projectImage);
    holder.projectDeadline = (TextView) vi.findViewById(R.id.projectdeadline);

    vi.setTag(holder);
} else {
    holder = (ViewHolder) vi.getTag();
}
if (projectItems.size() <= 0) {
    [...]
} else {
     [...]
    }
}
return vi;
}

}