ListActivity 和 Activity 的奇怪行为

发布于 2024-11-19 13:41:05 字数 6676 浏览 1 评论 0 原文

我正在为 Android 实现一个应用程序,在使用来自互联网的数据填充列表视图时遇到内存问题。出现的一个奇怪的错误似乎是由于缺乏回收引起的通常应该发生的行,如Google I/O 2009。

当我在 http://android.amberfog.com/?p=296 运行代码时,一切都运行顺利,行视图被回收,并且列表视图得到了优化使用。

我现在想在另一个活动中使用 listView,该活动将包含更多内容,因此仅扩展 ListActivity 的类是不够的。因此,我有一个具有以下代码的 Activity:

public class MultipleItemsList extends Activity {
    private Context mContext;
    private MyCustomAdapter mAdapter;
    private ListView listView;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mContext = this;

        listView = (ListView) findViewById(R.id.listView);

        mAdapter = new MyCustomAdapter(mContext);

        for (int i = 1; i < 50; i++) {
            mAdapter.addItem("item " + i);
        }

        listView.setAdapter(mAdapter);
    }  
}

main.xml 也没什么奇特的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"/>

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

上一个链接建议的 ListActivity 如下:

public class MultipleItemsList extends ListActivity {
    private Context mContext;
    private MyCustomAdapter mAdapter;
    private ListView listView;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;

        mAdapter = new MyCustomAdapter(mContext);

        for (int i = 1; i < 50; i++) {
            mAdapter.addItem("item " + i);
        }
        setListAdapter(mAdapter);
    }
}

在两种情况下适配器均如下:

public class MyCustomAdapter extends BaseAdapter {
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;
    private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

    private ArrayList<String> mData = new ArrayList<String>();
    private LayoutInflater mInflater;

    private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

    public static class ViewHolder {
        public TextView textView;
    }

    public MyCustomAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    public void addItem(final String item) {
        mData.add(item);
        notifyDataSetChanged();
    }

    public void addSeparatorItem(final String item) {
        mData.add(item);
        // save separator position
        mSeparatorsSet.add(mData.size() - 1);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_MAX_COUNT;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public String getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int type = getItemViewType(position);
        Log.d("myCA", "getView " + position + " " + convertView + " type = " + type);
        if (convertView == null) {
            holder = new ViewHolder();
            switch (type) {
                case TYPE_ITEM:
                    convertView = mInflater.inflate(R.layout.item1, null);
                    holder.textView = (TextView)convertView.findViewById(R.id.text);
                    break;
                case TYPE_SEPARATOR:
                    convertView = mInflater.inflate(R.layout.item2, null);
                    holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
                    break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.textView.setText(mData.get(position));
        return convertView;
    }
}

所以,我的问题是为什么在第二个中在 ListActivity 的情况下,视图回收没有问题,而在 Activity 的实现中,回收却搞砸了?

日志显示,当使用 ListActivity 时,所有内容都被正确回收:

    07-13 10:14:25.277: DEBUG/myCA(2336): getView 0 null type = 0
    07-13 10:14:25.277: DEBUG/myCA(2336): getView 1 null type = 0
    07-13 10:14:25.277: DEBUG/myCA(2336): getView 2 null type = 0
    07-13 10:14:25.277: DEBUG/myCA(2336): getView 3 null type = 0
    07-13 10:14:25.287: DEBUG/myCA(2336): getView 4 null type = 0
    07-13 10:14:25.287: DEBUG/myCA(2336): getView 5 null type = 0
    07-13 10:14:27.887: DEBUG/myCA(2336): getView 6 null type = 0
    07-13 10:14:28.047: DEBUG/myCA(2336): getView 7 android.widget.LinearLayout@40522c30 type = 0
    07-13 10:14:28.267: DEBUG/myCA(2336): getView 8 android.widget.LinearLayout@405236b8 type = 0
...
...
...

在我的 Activity 实现中,我看到以下内容:

07-13 10:11:47.517: DEBUG/myCA(2296): getView 0 null type = 0
07-13 10:11:47.517: DEBUG/myCA(2296): getView 1 android.widget.LinearLayout@405221e8 type = 0
07-13 10:11:47.517: DEBUG/myCA(2296): getView 2 android.widget.LinearLayout@405221e8 type = 0
07-13 10:11:47.517: DEBUG/myCA(2296): getView 3 android.widget.LinearLayout@405221e8 type = 0
07-13 10:11:47.527: DEBUG/myCA(2296): getView 4 android.widget.LinearLayout@405221e8 type = 0
07-13 10:11:47.527: DEBUG/myCA(2296): getView 5 android.widget.LinearLayout@405221e8 type = 0
07-13 10:11:47.547: DEBUG/myCA(2296): getView 0 android.widget.LinearLayout@405221e8 type = 0
07-13 10:11:47.547: DEBUG/myCA(2296): getView 1 null type = 0
07-13 10:11:47.547: DEBUG/myCA(2296): getView 2 null type = 0
07-13 10:11:47.557: DEBUG/myCA(2296): getView 3 null type = 0
...
...
...

回收明显存在问题。

当父活动只是一个简单的活动而不是 ListActivity 时,我是否缺少一些可以使 listView 正常运行的东西? 过去有人遇到过类似的事情吗?

感谢您抽出时间。

I'm implementing an application for android and I run into memory issues while populating a list view with data from the internet. A strange error that occurs seems to be caused by the lack of recycling of the rows as it should normally happen as it is stated at Google I/O 2009.

When I run the code at http://android.amberfog.com/?p=296, everything is running smoothly, the row views are recycled and the listView is optimally used.

I now want to use the listView within another activity that will have much more things inside, therefore a class that extends just ListActivity, is not enough. So, I have an Activity that has the following code:

public class MultipleItemsList extends Activity {
    private Context mContext;
    private MyCustomAdapter mAdapter;
    private ListView listView;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mContext = this;

        listView = (ListView) findViewById(R.id.listView);

        mAdapter = new MyCustomAdapter(mContext);

        for (int i = 1; i < 50; i++) {
            mAdapter.addItem("item " + i);
        }

        listView.setAdapter(mAdapter);
    }  
}

The main.xml is nothing fancy also:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"/>

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

The ListActivity that the previous link proposes is the following:

public class MultipleItemsList extends ListActivity {
    private Context mContext;
    private MyCustomAdapter mAdapter;
    private ListView listView;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;

        mAdapter = new MyCustomAdapter(mContext);

        for (int i = 1; i < 50; i++) {
            mAdapter.addItem("item " + i);
        }
        setListAdapter(mAdapter);
    }
}

The adapter is the following in both cases:

public class MyCustomAdapter extends BaseAdapter {
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;
    private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

    private ArrayList<String> mData = new ArrayList<String>();
    private LayoutInflater mInflater;

    private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

    public static class ViewHolder {
        public TextView textView;
    }

    public MyCustomAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    public void addItem(final String item) {
        mData.add(item);
        notifyDataSetChanged();
    }

    public void addSeparatorItem(final String item) {
        mData.add(item);
        // save separator position
        mSeparatorsSet.add(mData.size() - 1);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_MAX_COUNT;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public String getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int type = getItemViewType(position);
        Log.d("myCA", "getView " + position + " " + convertView + " type = " + type);
        if (convertView == null) {
            holder = new ViewHolder();
            switch (type) {
                case TYPE_ITEM:
                    convertView = mInflater.inflate(R.layout.item1, null);
                    holder.textView = (TextView)convertView.findViewById(R.id.text);
                    break;
                case TYPE_SEPARATOR:
                    convertView = mInflater.inflate(R.layout.item2, null);
                    holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
                    break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.textView.setText(mData.get(position));
        return convertView;
    }
}

So, my question is why in the second case of the ListActivity the view recycling takes place without problems, while at the implementation of the Activity, recycling gets screwed up ?

The Log shows that when the ListActivity is used, everything is recycled correctly:

    07-13 10:14:25.277: DEBUG/myCA(2336): getView 0 null type = 0
    07-13 10:14:25.277: DEBUG/myCA(2336): getView 1 null type = 0
    07-13 10:14:25.277: DEBUG/myCA(2336): getView 2 null type = 0
    07-13 10:14:25.277: DEBUG/myCA(2336): getView 3 null type = 0
    07-13 10:14:25.287: DEBUG/myCA(2336): getView 4 null type = 0
    07-13 10:14:25.287: DEBUG/myCA(2336): getView 5 null type = 0
    07-13 10:14:27.887: DEBUG/myCA(2336): getView 6 null type = 0
    07-13 10:14:28.047: DEBUG/myCA(2336): getView 7 android.widget.LinearLayout@40522c30 type = 0
    07-13 10:14:28.267: DEBUG/myCA(2336): getView 8 android.widget.LinearLayout@405236b8 type = 0
...
...
...

At my implementation of Activity, I see the following:

07-13 10:11:47.517: DEBUG/myCA(2296): getView 0 null type = 0
07-13 10:11:47.517: DEBUG/myCA(2296): getView 1 android.widget.LinearLayout@405221e8 type = 0
07-13 10:11:47.517: DEBUG/myCA(2296): getView 2 android.widget.LinearLayout@405221e8 type = 0
07-13 10:11:47.517: DEBUG/myCA(2296): getView 3 android.widget.LinearLayout@405221e8 type = 0
07-13 10:11:47.527: DEBUG/myCA(2296): getView 4 android.widget.LinearLayout@405221e8 type = 0
07-13 10:11:47.527: DEBUG/myCA(2296): getView 5 android.widget.LinearLayout@405221e8 type = 0
07-13 10:11:47.547: DEBUG/myCA(2296): getView 0 android.widget.LinearLayout@405221e8 type = 0
07-13 10:11:47.547: DEBUG/myCA(2296): getView 1 null type = 0
07-13 10:11:47.547: DEBUG/myCA(2296): getView 2 null type = 0
07-13 10:11:47.557: DEBUG/myCA(2296): getView 3 null type = 0
...
...
...

where there is clearly problem with the recycling.

Am I missing something that would make the listView behave properly when the parent activity is just a simple activity and not a ListActivity ?
Has anyone come across something like that in the past ?

Thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

并安 2024-11-26 13:41:05

ActivityListActivity 都应该没问题。我相信您通过 mContext 引用泄漏了整个活动。删除它,看看会发生什么!

Both Activity and ListActivity should be fine. I believe you're leaking the entire activity through the mContext reference. Remove it and see what happens!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文