Est-ce que vous pouvez m'aider s'il vous plaît ?

Voilà, ça fait 1 semaine que je cherche à optimiser mon code:
j'ai une listview en Android qui récupère des données en ligne de format JSON.
Elle fonctionne déjà mais le "hic" c'est que ça prend trop de temps pour se charger (se sont les photos qui ralentissent gravement le processus d'affichage).
Je peine à trouver la solution, et j'ai découvert qu'il existait une méthode appelée AsyncTask, mais je ne sais comment faire pour l'adapter à mon code.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.ps;
 
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
 
import com.ps.parseActivites.ContainerDataActivites;
import com.ps.parseActivites.FeedActivites;
 
public class Activites extends Activity {
 
private ListView listViewActivites	 =	null ;
 
private String title = null ;
private String link	 =	null	;
private String image	 =	null	;
private String description	 =	null	;
private String pubDate	 =	null	;
private String address = null ;
private String lieu	 =	null	;
private String contactname	 =	null	;
private String contactaddress	=	null	;
private String contactphone	 =	null	;
private String contactmail	 =	null	;
 
@SuppressWarnings("unused")
private Context context;
private ErrorStatus status;
enum ErrorStatus {
NO_ERROR, ERROR_1, ERROR_2
};	
public static final int MSG_ERR = 0;
public static final int MSG_CNF = 1;
public static final int MSG_IND = 2;
private boolean connectionActif = false;
protected ProgressDialog mProgressDialog = null;
 
private SimpleAdapter mScheduleActivites;
 
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.flipper);
 
Compute();
}
 
public ErrorStatus getActivites(){
connectionActif = isConnected();
 
if(connectionActif){
 
ArrayList<FeedActivites> feeds = ContainerDataActivites.getFeeds();
ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map;
 
for (FeedActivites feed : feeds) {
link	 =	feed.getLink();
title	 =	feed.getTitle();
image	 =	feed.getImage();
description	 =	feed.getDescription();
pubDate	 =	feed.getPubDate().replace(" GMT", "").replace("Mon,", "").replace("Tue,", "").replace("Wed,", "").replace("Thu,", "").replace("Fri,", "").replace("Sat,", "").replace("Sun.,", "");
address	 =	feed.getAddress();
lieu	 =	feed.getLieu();
contactname	 =	feed.getContactName();
contactaddress	=	feed.getContactAddress();
contactmail	 =	feed.getContactMail();
contactphone	=	feed.getContactPhone();
 
map = new HashMap<String, Object>();
map.put("titre",title);
map.put("image",image);
map.put("date",pubDate);
map.put("link",link);
map.put("description",description.replace("é", "é"));
map.put("address",address);
map.put("lieu",lieu);
map.put("contactname",contactname);
map.put("contactaddress",contactaddress.replace("&nbsp;", " "));
map.put("contactmail",contactmail);
map.put("contactphone",contactphone);
 
URL pictureURL = null;
Bitmap bitmap = null;
 
try {
pictureURL = new URL(image.toString());
bitmap = BitmapFactory.decodeStream(pictureURL.openStream());
map.put("littleAvatar",bitmap);
} catch (Exception e) {
e.printStackTrace();
}
listItem.add(map); 
 
mScheduleActivites = new SimpleAdapter (Activites.this.getBaseContext(), listItem, R.layout.affichage_list,
new String[] {"date","titre"}, new int[] {R.id.date,R.id.titre});
mScheduleActivites.setViewBinder(new MyViewBinder());
 
}
 
return ErrorStatus.NO_ERROR;
}
else return ErrorStatus.ERROR_1;
}
 
private void Compute(){
mProgressDialog = ProgressDialog.show(Activites.this, "","", true);
Thread th = new Thread((new Runnable() {
public void run() {
Message msg = null;
String progressBarData = "Chargement ...";
msg = mHandler.obtainMessage(MSG_IND, (Object) progressBarData);
mHandler.sendMessage(msg);
status = getActivites();
if(ErrorStatus.NO_ERROR == status){
msg = mHandler.obtainMessage(MSG_CNF,"Parsing and computing ended successfully !");
mHandler.sendMessage(msg);
}
if(ErrorStatus.ERROR_1 == status){
msg = mHandler.obtainMessage(MSG_ERR,"Erreur de connexion à internet!");
mHandler.sendMessage(msg);
}
}
}));
th.start();
}
 
Handler mHandler = new Handler() {
public void handleMessage(Message msg){
switch (msg.what){
case MSG_IND:
if (mProgressDialog.isShowing()){
mProgressDialog.setMessage(((String) msg.obj));
}
break;
case MSG_ERR:
if (mProgressDialog.isShowing()){
mProgressDialog.dismiss();
}
break;
case MSG_CNF:
Button activites_ps_btn = (Button) findViewById(R.id.u_ps_activites_details);
activites_ps_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
 
startActivity(new Intent("com.ps.Main"));
} 
});
 
 
if (mProgressDialog.isShowing()){
listViewActivites = (ListView) findViewById(R.id.list_activites);
listViewActivites.setAdapter(mScheduleActivites);
listViewActivites.setOnItemClickListener(new OnItemClickListener() { 
@SuppressWarnings("unchecked")
public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
final HashMap<String, String> activitesItem = (HashMap<String, String>) mScheduleActivites.getItem(position);
 
URL pictureURL;
try {
pictureURL = new URL(activitesItem.get("image"));
ImageView avatar = (ImageView) findViewById(R.id.activites_details_image); 
Bitmap bitmap = BitmapFactory.decodeStream(pictureURL.openStream());
avatar.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
});
mProgressDialog.dismiss();
}
 
break;
default: 
break;
}
}
};
 
@SuppressWarnings("static-access")
public boolean isConnected(){
Context context = getApplicationContext();
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
if (networkInfo != null){
for (int i = 0; i < networkInfo.length; i++){
if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED){
return true;
}
}
}
return false;
}
}
merci d'avance