我正在使用异步代码在 for 循环中从 Web 获取图像。请建议我可以做些什么来快速生成这个列表。我将此数据传递给列表视图。
private class CreateItems extends AsyncTask<Void, Void, Void>{
protected Void doInBackground(Void... params) {
android.os.Debug.waitForDebugger();
SharedPreferences sp = getSharedPreferences(UserDataManagement.CATALOG, 0);
String items = sp.getString("items", "");
try {
JSONArray array = new JSONArray(items);
for(int i=0;i<array.length();i++){
JSONObject item = array.getJSONObject(i);
String id = item.getString("id");
String firstName = item.getString("first_name");
String lastName = item.getString("last_name");
String imageUrl = item.getString("image");
InputStream in = new java.net.URL(imageUrl).openStream();
Bitmap imageBitmap = BitmapFactory.decodeStream(in);
in.close();
Item item = new Item(id, firstName, lastName, imageUrl, imageBitmap);
itemList.add(item);
/* if(i!=0 && i%10==0){
runOnUiThread(new Runnable() {
public void run() {
adapter.notifyDataSetChanged();
}
});
}*/
}
}
catch(Exception ex){
ex.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
adapter.notifyDataSetChanged();
}
}