Android ListView内存使用情况

时间:2015-10-13 10:05:38

标签: android listview memory

我不知道问题出在哪里,因为我来到这里。

指令:

我有一个ListView,我从Internet获取数据。 在URL中,我有一个JSON-String,但没有问题。我从URL中获取数据没有问题,而且还有一个图片网址,我将其与Picasso一起加载到ListView中,并附带其他数据。

解决问题

我有3分导航。每次当用户按下其中一个时,我从属于该项的URL加载信息(example.php?json = 1),所以我不会更改页面,我只是删除了适配器(list.clear)并重新加载数据。

问题

每次在导航时按下相同项目时,MEMORY都会增加1mb。

示例内存使用率为80mb,当我按导航"示例1"它得到81mb,当我按下它得到82mb等等。

代码

MainActivity.java

private void loadGames(final String title, String pageUrl, final boolean reset) {
    if(reset) resetGames();
    RequestQueue queue = Volley.newRequestQueue(this);
    StringRequest stringRequest = new StringRequest(Request.Method.GET, pageUrl, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            if(response.length() > 0) {
                Gson gson = new GsonBuilder().create();
                RootObject object = gson.fromJson(response, RootObject.class);

                for (int i = 0; i < object.result.size(); i++) {

                    ... BLA BLA BLA

                    articles.add(new ArticleItem(object.result.get(i).id, object.result.get(i).title, (int) days, object.result.get(i).value, object.result.get(i).picURL, object.result.get(i).url, object.result.get(i).promoter, object.result.get(i).description, object.result.get(i).participationDate, participated));
                    listAdapter.notifyDataSetChanged();
                }
                if(reset) YoYo.with(Techniques.BounceInUp).duration(1500).playOn(articleView);
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    queue.add(stringRequest);
    actionBar.setPageTitle(title);
}

修改

ArticleAdapter.java

public class ArticleAdapter extends ArrayAdapter<ArticleItem> {

    private LayoutInflater inflater;
    private List<ArticleItem> articles;

    public ArticleAdapter(Context context, List<ArticleItem> articles) {
        super(context, 0, articles);
        inflater = LayoutInflater.from(getContext());
        this.articles = articles;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View view;
        if(convertView == null) {
            view = inflater.inflate(R.layout.article_layout, null);
        } else {
            view = convertView;
        }

        TextView title = (TextView) view.findViewById(R.id.title);
        title.setText(articles.get(position).getTitle());

        TextView days = (TextView) view.findViewById(R.id.days);

        TextView desc = (TextView) view.findViewById(R.id.desc);
        desc.setText("Promoter: " + articles.get(position).getPromoter());

        Picasso.with(getContext()).load(R.drawable.ablaufzeit_weiss).into((ImageView)view.findViewById(R.id.letzte_chance_icon));
        Picasso.with(getContext()).load(R.drawable.gesamtwert_weiss).into((ImageView)view.findViewById(R.id.geld_icon));

        int amount = articles.get(position).getDays();
        String tage = (amount > 1) ? "Days" : "Day";
        days.setText("In " + amount + " " + tage);
        if(amount == 0)
            days.setText("Ends today");
        else if(amount < 0)
            days.setText("End");

        TextView worth = (TextView) view.findViewById(R.id.worth);
        worth.setText(articles.get(position).getWorth() + " €");

        ImageView image = (ImageView) view.findViewById(R.id.image);
        Picasso.with(getContext()).load(articles.get(position).getImgUrl()).placeholder(R.drawable.placeholder).into(image);

        ViewGroup articleInfo = (ViewGroup) view.findViewById(R.id.article_info);
        articleInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setTitle(Html.fromHtml("<font color='black'>" + articles.get(position).getTitle() + "</font>"));
                builder.setMessage(articles.get(position).getDesc())
                        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {

                            }
                        });
                builder.create().show();
            }
        });

        if(articles.get(position).isParticipated()) {
            view.findViewById(R.id.participated).setVisibility(View.VISIBLE);
            TextView participatedText = (TextView) view.findViewById(R.id.participated_text);
            participatedText.setTypeface(FontCache.get("Action_Man.ttf", getContext()));
            participatedText.setText("Participated\nam " + articles.get(position).getParticipationDate());
        } else {
            view.findViewById(R.id.participated).setVisibility(View.GONE);
        }
        return view;
    }
}

resetGames()

private void resetGames() {
    articles.clear();
    listAdapter.notifyDataSetChanged();
}

1 个答案:

答案 0 :(得分:0)

你应该有一个应用程序的任务

每次调用loadGames()时,都会创建新实例

 RequestQueue queue = Volley.newRequestQueue(this);

创建Quene管理器

public class QueueManager {

private static final String TAG = QueueManager.class.getName();
private static QueueManager queueManager;
private Context ctx;
private RequestQueue requestQueue;

private QueueManager(Context context) {
    ctx = context.getApplicationContext();
    requestQueue = Volley.newRequestQueue(ctx);
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

        @Override
        public boolean verify(String hostname, SSLSession session) {

            return true;
        }
    });
}

public static synchronized QueueManager getInstance(Context context) {
    if (queueManager == null) {
        queueManager = new QueueManager(context);
    }
    return queueManager;
}

public RequestQueue getRequestQueue() {
    return requestQueue;
}

您可以访问的活动

quene = QueueManager.getInstance(this).getRequestQueue();