0

In a nutshell, I have a ListView and when I scroll through it, I get grow heaps and on the device I can see it hooking. I've read something about recycling Bitmaps, but I haven't found a solution for doing this in my project.

TeamFragment.java:

public class TeamFragment extends ListFragment implements OnItemClickListener {

    private TeamListAdapter adapter;

    public TeamFragment() {}

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        this.fragmentNumber = getArguments().getInt(ARG_FRAGMENT_NUMBER);

        adapter = new TeamListAdapter(this.getActivity());
        setListAdapter(adapter);

        getListView().setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view,
            int position, long id) {
        Intent intent = new Intent(view.getContext(), TeamDetailActivity.class);
        Bundle extra = new Bundle();
        extra.putInt("drawable", adapter.getItem(position).getDrawable());
        extra.putString("name", adapter.getItem(position).getName());
        extra.putString("title", adapter.getItem(position).getTitle());
        extra.putString("description", adapter.getItem(position).getDescription());
        intent.putExtra("extra", extra);
        startActivityForResult(intent, 0);

    }

}

TeamListAdapter.java

public class TeamListAdapter extends BaseAdapter {

    private final LayoutInflater inflater;
    private final List<HumanItem> humanItems;
    private String[] teamname_array;
    private String[] teamname_extra_array;
    private String[] teamdescription_array;
    private String[] teamid_array;

    public TeamListAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        humanItems = new ArrayList<HumanItem>();

        teamid_array = context.getResources().getStringArray(
                R.array.teamids);               
        teamname_array = context.getResources().getStringArray(
                R.array.teamnames);
        teamname_extra_array = context.getResources().getStringArray(
                R.array.teamnames_extra);
        teamdescription_array = context.getResources().getStringArray(
                R.array.teamdescriptions);


        HumanItem tmp;
        int resId;
        int i = 0;
        for(String name : teamid_array) {
            resId = context.getResources().getIdentifier("team_" + name, "drawable", context.getPackageName());
            tmp = new HumanItem(name, resId, teamname_array[i], teamname_extra_array[i], teamdescription_array[i]);
            String TAG = TeamListAdapter.class.getSimpleName();
            Log.d(TAG , "team_" + name);
            i++;
            humanItems.add(tmp);
        }


    }

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

    @Override
    public HumanItem getItem(int position) {
        return humanItems.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        // build contentView if necessary
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.fragment_news, parent,
                    false);

            // create holder
            holder = new ViewHolder();

            holder.bubble = (TextView) convertView
                    .findViewById(R.id.news_bubble);
            holder.title = (TextView) convertView.findViewById(R.id.news_title);
            holder.summary = (TextView) convertView
                    .findViewById(R.id.news_summary);
            holder.teaser = (ImageView) convertView
                    .findViewById(R.id.news_drawable);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // Context context = parent.getContext();
        HumanItem humanItem = getItem(position);
        holder.bubble.setVisibility(View.GONE);
        holder.title.setText(humanItem.getTitle());
        holder.summary.setText(humanItem.getSummary());
        holder.teaser.setImageResource(humanItem.getDrawable());

        return convertView;

    }

    static class ViewHolder {
        TextView bubble, title, summary;
        ImageView teaser;
    }

}

One of the many grow heaps:

12-31 13:49:31.756: I/dalvikvm-heap(27543): Grow heap (frag case) to 23.180MB for 2569892-byte allocation

So where do I have to recycle Bitmaps? Or is there another way of preventing grow heaps?

vigonotion
  • 1,164
  • 2
  • 10
  • 21

2 Answers2

0

Actually it is not good to do what you want as eventually you will get the outOfMemoryError. To handle bitmap You can see my answer here.It will be useful to handle your bitmap efficiently. Just check it out..!

How to make application more responsive which uses several bitmaps?

Community
  • 1
  • 1
Kailash Dabhi
  • 3,473
  • 1
  • 29
  • 47
0

If you need to show a list of images, I suggest to check this library, I used this library and works really fine with 0 freezes and estable heap: Universal Image Loader

Ezrou
  • 438
  • 6
  • 14