我不知道問題出在哪裏,因爲我來到了這裏。Android ListView內存使用
指令:
我有一個ListView,我從互聯網獲取的數據。 在URL中,我有一個JSON字符串,但沒有問題。我從URL中免費獲得數據,並且還有一個Image URL,我將它們與Picasso一起加載到帶有其他數據的ListView中。
將問題
我有3點導航。每當用戶按其中一個按鈕時,我從屬於該項目的URL加載信息(example.php?json = 1),所以我不更改頁面,我只是刪除適配器(list.clear)並重新加載數據。
問題
每次當我按下導航同一項目時,存儲器得到更高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();
}
郵適配器代碼 –
發佈適配器代碼。這可能是您列表中的問題。您應該考慮在添加新數據之前嘗試使用list.clear()。 – Ritesh
完成,適配器代碼現在在那裏。 – Unknown