Strange behavior of ListActivity and Activity
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);
hol开发者_StackOverflowder.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.
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!
精彩评论