我寫了一個從JSON中獲取圖像的類。它正在工作,bur存在內存問題。當我向下滾動應用程序時,它正在泄漏內存中。這裏是我的代碼。Android中的ListView內存泄漏
package com.nexum.senddata;
public class MainActivity extends Activity {
private ListView list;
private MyAdapter adapter;
private final String parsingUrl = "http://sametdede.com/expo/data.json";
private String tag_coord = "Coord";
private String tag_lat = "Lat";
private String tag_lon = "Lon";// Double
private String tag_image = "Image";
private String tag_InIzmir = "InIzmir";
private String tag_name = "Name";
private ProgressDialog pDialog;
private static int clickedItemPosition = -1;
private final int REQUEST_CODE_DETAIL = 1;
ArrayList<CoordItem> items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
items = new ArrayList<CoordItem>();
adapter = new MyAdapter(this, R.layout.list_item, items);
list = (ListView) findViewById(R.id.exampList);
list.setAdapter(adapter);
pDialog = new ProgressDialog(this);
pDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
pDialog.setMessage("Veriler alınıyor...");
new ListViewLoad().execute();
list.setOnItemClickListener(new OnItemClickListener() {
@SuppressLint("ShowToast")
@Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
/*
* String bulunma = ""; if (items.get(position).inIzmir) {
* bulunma+="İzmir de bulundu."; }else{
* bulunma+="İzmir de bulunmadı."; } String
* s="Name:"+items.get(position).name+"\nInIzmir:"+bulunma;
* Toast.makeText(getApplicationContext(), s,
* Toast.LENGTH_LONG).show();
*/
clickedItemPosition = position;
Intent myIntent = new Intent(MainActivity.this,
DetailActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable(
"parcelable_key",
new CoordItem(items.get(position).name, items
.get(position).img, items.get(position).lat,
items.get(position).lon,
items.get(position).inIzmir));
myIntent.putExtras(bundle);
// Güncelleme olayı burada başlıyor
// startActivityForResult(myIntent, REQUEST_CODE_DETAIL);
startActivityForResult(myIntent, REQUEST_CODE_DETAIL);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_DETAIL && resultCode == RESULT_OK
&& data != null) {
CoordItem coorditem = new CoordItem(data.getStringExtra("name"),
items.get(clickedItemPosition).img, Double.parseDouble(data
.getStringExtra("lat")), Double.parseDouble(data
.getStringExtra("lon")),
items.get(clickedItemPosition).inIzmir);
items.set(clickedItemPosition, coorditem);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Guncelleme yapıldı.",
Toast.LENGTH_SHORT).show();
}
/*
* CoordItem coorditem = new CoordItem(data.getStringExtra("name"),
* items.get(clickedItemPosition).img,
* Double.parseDouble(data.getStringExtra("lat")),Double.parseDouble(
* data.getStringExtra("lon")), items.get(clickedItemPosition).inIzmir);
*
* items.set(clickedItemPosition, coorditem);
* adapter.notifyDataSetChanged();
* Toast.makeText(getApplicationContext(), "Guncelleme yapıldı.",
* Toast.LENGTH_SHORT).show();
*/
}
private class ListViewLoad extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
String json = getStringFromURL(parsingUrl);
try {
final JSONArray jArray = new JSONArray(json);
for (int i = 0; i < jArray.length(); i++) {
JSONObject c = jArray.getJSONObject(i);
JSONObject coord = c.getJSONObject(tag_coord);
double lat = coord.getDouble(tag_lat);
double lon = coord.getDouble(tag_lon);
String image = c.getString(tag_image);
boolean InIzmir = c.getBoolean(tag_InIzmir);
String name = c.getString(tag_name);
CoordItem item = new CoordItem(name, image, lat, lon,
InIzmir);
items.add(item);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
adapter.notifyDataSetChanged();
pDialog.dismiss();
}
}
private class MyAdapter extends ArrayAdapter<CoordItem> {
private LayoutInflater inflater;
public MyAdapter(Context context, int textViewResourceId,
ArrayList<CoordItem> objects) {
super(context, textViewResourceId, objects);
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
if (items != null)
return items.size();
else
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewholder = null;
if (convertView == null) {
viewholder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item, parent,
false);
viewholder.listItemImage = (ImageView) convertView
.findViewById(R.id.listItemImage);
viewholder.listItemName = (TextView) convertView
.findViewById(R.id.listItemName);
viewholder.listItemInIzmir = (TextView) convertView
.findViewById(R.id.listItemInIzmir);
viewholder.listItemLatitude = (TextView) convertView
.findViewById(R.id.listItemLatitude);
viewholder.listItemLongitude = (TextView) convertView
.findViewById(R.id.listItemLongitude);
convertView.setTag(viewholder);
} else {
viewholder = (ViewHolder) convertView.getTag();
}
viewholder.listItemName.setText(items.get(position).name);
if (items.get(position).inIzmir) {
viewholder.listItemInIzmir.setText("Izmirde.");
} else {
viewholder.listItemInIzmir.setText("Izmirde degil.");
}
try {
viewholder.listItemImage
.setImageDrawable(getDrawableFromUrl(new URL(items.get(
position).getImg())));
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
public Drawable getDrawableFromUrl(URL url) {
try {
InputStream is = (InputStream) url.getContent();
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (Exception e) {
return null;
}
}
}
/**
* Burada layoutinflater da kullandığımız layoutun componentlerini
* yazıyoruz.Bu da bizim list_item.xml imizdir.
*/
static class ViewHolder {
ImageView listItemImage;
TextView listItemName, listItemInIzmir, listItemLatitude,
listItemLongitude;
}
public String getStringFromURL(String url) {
// Making HTTP request
String json = "";
InputStream is = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
// return JSON String
return json;
}
}
我在哪裏可以閱讀和獲取的圖像用於執行速度更快,而不是內存泄漏?
我從JSON獲取網址。那麼如何根據我的代碼初始化ImageLoader? – emrerme
imageLoader.displayImage(imageurl,image,options);如果您使用的是圖像加載器,請使用上面的圖像加載器。傳遞圖像網址到你的cusotm適配器,並提供URL像url [position] – Raghunandan
No no。我怎樣才能初始化ImageLoader類。它需要一些參數ImageLoader imageloader = new ImageLoader(?);在我的代碼中,我可以放什麼?在您的適配器構造函數中感謝 – emrerme