ListActivity 和 Activity 的奇怪行为
我正在为 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 正常运行的东西? 过去有人遇到过类似的事情吗?
感谢您抽出时间。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Activity
和ListActivity
都应该没问题。我相信您通过mContext
引用泄漏了整个活动。删除它,看看会发生什么!Both
Activity
andListActivity
should be fine. I believe you're leaking the entire activity through themContext
reference. Remove it and see what happens!