在Android ListView中成长堆

时间:2013-12-31 13:00:25

标签: java android listview bitmap

简而言之,我有一个ListView,当我滚动它时,我会变得越来越大,在设备上我可以看到它挂钩。我已经阅读了有关回收Bitmaps的内容,但我还没有找到在我的项目中执行此操作的解决方案。

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;
    }

}

众多成长中的一个:

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

那么我在哪里可以回收Bitmaps?或者还有另一种防止成长的方法吗?

2 个答案:

答案 0 :(得分:0)

实际上做你想做的事并不好,最终你会得到outOfMemoryError。 处理位图你可以在这里看到我的答案。有效处理你的位图会很有用。 检查一下..!

How to make application more responsive which uses several bitmaps?

答案 1 :(得分:0)

如果你需要显示一个图像列表,我建议检查一下这个库,我使用这个库并且工作得非常好,有0个冻结和可用的堆:Universal Image Loader