我從URL獲取JSON響應並將其轉換爲字符串。我從響應中獲取圖片的網址。我想從URL下載該圖像並將其顯示到ListView中。但是這需要很長時間,並且直到下載所有圖像才顯示空白屏幕。屏幕顯示將近40-50秒。此外,ListView不是很流暢。我怎樣才能防止黑屏出現?從Url下載圖像並在ListView中顯示時出現空白屏幕android
這裏是我的代碼: -
String registerContet = "myUrl";
String items;
try
{
items = new FetchItems().execute(registerContet).get();
pDialog = new ProgressDialog(this).show(Home.this, "Fetching news items", "Please wait..");
JSONArray jObject = new JSONArray(items);
for (int i = 0; i < jObject.length(); i++)
{
JSONObject menuObject = jObject.getJSONObject(i);
String title= menuObject.getString("Title");
String description= menuObject.getString("BodyText");
String thumbnail= menuObject.getString("ThumbnailPath");
String newsUrl = menuObject.getString("Url");
String body = menuObject.getString("Body");
String newsBigImage = menuObject.getString("ImageBlobUrls");
map = new HashMap<String,String>();
map.put(SOURCETITLE, title);
map.put(TITLE, description);
map.put(THUMBNAILPATH, thumbnail);
map.put(BODY, body);
map.put(URL, newsUrl);
map.put(IMAGEBLOBURLS,newsBImage);
myNList.add(map);
}
itemsAdapter = new LazyAdapter(Home.this, myNList);
if(pDialog!=null && pDialog.isShowing())
{
pDialog.dismiss();
}
nList.setAdapter(itemsAdapter);
nList.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0,
View arg1, int position, long arg3)
{
// TODO Auto-generated method stub
myDialog = new ProgressDialog(Home.this).show(Home.this, "Fetching news..", "Just a moment");
HashMap<String, String> myMap = myNList.get(position);
Intent nIntent = new Intent(Home.this,NDetails.class);
newsIntent.putExtra("NItems", myMap);
startActivity(nIntent);
}
});
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private class FetchItems extends AsyncTask<String, String, String>
{
// TODO Auto-generated method stub
ProgressDialog myDialog;
@Override
protected String doInBackground(String... params)
{
// TODO Auto-generated method stub
HttpResponse response =null;
String resultString = "";
String myResponseBody = "" ;
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpGet request = new HttpGet(params[0]);
try
{
response = httpClient.execute(request);
if(response.getStatusLine().getStatusCode()== 200)
{
HttpEntity entity = response.getEntity();
if (entity != null)
{
InputStream inputStream = entity.getContent();
myResponseBody = convertToString(inputStream);
}
}
}
catch(Exception e)
{
}
return myResponseBody;
}
@Override
protected void onPostExecute(String result)
{
// TODO Auto-generated method stub
super.onPostExecute(result);
/*if(myDialog.isShowing())
{
myDialog.dismiss();
}*/
}
@Override
protected void onPreExecute()
{
// TODO Auto-generated method stub
super.onPreExecute();
/*myDialog = new ProgressDialog(Home.this);
myDialog.setMessage("Loading");
myDialog.show();*/
}
誰能告訴我怎樣才能解決這個問題。 謝謝
你也可以使用通用圖像加載器類來進行延遲加載。 – 2014-03-28 10:55:35