2013-05-31 58 views
0

我使用Asynctask下載圖像,該圖像是從自定義適配器的getview方法執行的。這個自定義適配器設置爲一個列表視圖,但是沒有任何東西顯示在列表視圖中。 我想在主活動 上點擊進度條時顯示列表視圖,可能是什麼問題?請任何人都可以幫助我。 我的代碼是這樣的: @覆蓋 公共查看getView(INT位置,查看convertView,ViewGroup以及母公司){無法顯示列表視圖,asynctask用於在適配器中下載圖像

 inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
     if (convertView == null){ 
      convertView = inflater.inflate(R.layout.show_image, null); 
      imageview = (ImageView) convertView.findViewById(R.id.imageview); 
      convertView.setTag(imageview); 
      new DownloadTask().execute(images.get(position), imageview);     
     } 
     return convertView; 
    }  
} 

private class DownloadTask extends AsyncTask<Object, Integer, Bitmap>{ 
    ImageView iv; 

    @Override 
    protected Bitmap doInBackground(Object... params) {   

     String url = (String) params[0]; 
     iv = (ImageView) params[1];  
     Bitmap bmap = null; 

     try {    
      bmap = getBitmap(url); 
     } catch (IOException e) { 

      e.printStackTrace(); 
     } 
      return bmap; 
    } 

    public Bitmap getBitmap(String urlstring) throws IOException{ 

     URL _url = new URL(urlstring); 
     HttpURLConnection connection = (HttpURLConnection) _url.openConnection(); 

     InputStream stream = connection.getInputStream(); 

     Bitmap bitmap = BitmapFactory.decodeStream(stream); 

     return bitmap; 
    } 

    @Override 
    protected void onPostExecute(Bitmap result) {   
     super.onPostExecute(result); 

     iv.setImageBitmap(result);   

    } 

progress.setOnClickListener(新OnClickListener(){

 @Override 
     public void onClick(View v) { 
      progress.setVisibility(progress.GONE); 
      adapter = new ImageAdapter(getApplicationContext(), R.layout.show_image,imagenames); 

      listview.setAdapter(adapter); 
      adapter.notifyDataSetChanged(); 

      } 
    }); 
+0

您看過您的LogCat窗口嗎?你看到拋出任何異常嗎?您是否嘗試過在代碼中調試並停止在斷點處,以查看**代碼失敗的位置?請告訴我們。謝謝。 – Nate

回答

0
ImageView imageView; // Global variable 
//not ImageView in Asynctask, iv = (ImageView) params[1]; 

in doInBackground

try {    
    publishProgress(url); 
} catch (IOException e) { 
    //... 
} 

@Override  
protected void onProgressUpdate(String... urlstring) { 
    //do what you to in getBitmap 
    //but not return it 
    imageView.setImageBitmap(bitmap); 
} 

您應該使用onProgressUpdate