getView called several times even with layout_height="fill_parent"

251 Views Asked by At

I know there are already plenty of questions when it comes to getview() but I really can't figure out what's going on here.

I have an adapter where I dynamically add textViews :

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

    OrderForAdapter currentData = data.get(position); // getting data

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.order_detailsforlist, null);
    }

    if (currentData != null) {
        LinearLayout order_details_layout =
                (LinearLayout) convertView.findViewById(R.id.order_details_layout);
        // Adding the textView name of the person
        TextView labelName = new TextView(context);
        labelName.setText(currentData.getName());
        order_details_layout.addView(labelName);

        // looping through all "titres" and display all the titles
        ArrayList<ObjectForAdapter> listOfObjects = currentData.getObjects();
        for (int i = 0; i < listOfObjects.size(); i++) {
            TextView labelTitle = new TextView(context);
            labelTitle.setText(listOfObjects.get(i).getTitle());
            // Adding the title of each object
            order_details_layout.addView(labelTitle);
        }

    }

    return convertView;

}

XML :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.greederz.activities.ListOrdersActivity">

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@android:id/list" />
</LinearLayout>

When I first start the app, there is no problem, everything is displayed correctly and getView() is only called once.

However when I go to another activity and come back to this one, suddently getView is called 2 or 3 times in a row.

I also noticed that rotating the screen makes getView() being called once (so that works). So it must be something about the view being reused or the activity being not destroyed correctly.

The only answer I could find was layout_height="fill_parent" and i've changed it but no luck.

Please help :)

0

There are 0 best solutions below