0

我知道在 getview() 方面已经有很多问题,但我真的不知道这里发生了什么。

我有一个适配器,可以在其中动态添加 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>

当我第一次启动应用程序时,没有任何问题,一切都正确显示并且getView()只调用了一次。

但是,当我去另一个活动并回到这个活动时,突然getView被连续调用 2 或 3 次。

我还注意到旋转屏幕会使 getView() 被调用一次(这样就可以了)。所以它一定是关于视图被重用或活动没有被正确销毁的问题。

我能找到的唯一答案是 layout_height="fill_parent" 我已经改变了它但没有运气。

请帮忙 :)

4

0 回答 0