2013-02-07 112 views
3

我陷入了一個問題,我希望您能就如何改進我的代碼給我提出建議。我正在編碼有關從URL顯示圖像到列表視圖,但它沒有出現。這裏是我的代碼:在列表視圖中顯示URL中的圖像

public class MainActivity extends ListActivity { 

private String[] imgNames; 
private String[] imgUrls; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.activity_main); 

    String[] imgNames = {"ToDo","Gmail","LinkedIn","Facebook","Google Play"}; 
    this.imgNames = imgNames; 
    String[] imgUrls = { 
      "http://ww1.prweb.com/prfiles/2010/05/11/1751474/gI_TodoforiPadAppIcon512.png.jpg", 
      "http://cdn4.iosnoops.com/wp-content/uploads/2011/08/Icon-Gmail_large-250x250.png", 
      "http://kelpbeds.files.wordpress.com/2012/02/lens17430451_1294953222linkedin-icon.jpg?w=450", 
      "http://snapknot.com/blog/wp-content/uploads/2010/03/facebook-icon-copy.jpg", 
      "https://lh3.googleusercontent.com/-ycDGy_fZVZc/AAAAAAAAAAI/AAAAAAAAAAc/Q0MmjxPCOzk/s250-c-k/photo.jpg" 
    }; 
    this.imgUrls = imgUrls; 

    ListAdapter adapter = new ListAdapter(this, imgNames, imgUrls); 
    setListAdapter(adapter); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
    ImageView bmImage; 

    public DownloadImageTask(ImageView bmImage) { 
     this.bmImage = bmImage; 
    } 

    protected Bitmap doInBackground(String... urls) { 
     String urldisplay = urls[0]; 
     Bitmap mIcon11 = null; 
     try { 
      InputStream in = new java.net.URL(urldisplay).openStream(); 
      mIcon11 = BitmapFactory.decodeStream(in); 
     } catch (Exception e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 
     return mIcon11; 
    } 

    protected void onPostExecute(Bitmap result) { 
     bmImage.setImageBitmap(result); 
    } 
} 

public class ListAdapter extends ArrayAdapter<String> { 
    private final Context context; 
    private final String[] imgUrls; 
    private final String[] imgNames; 

    public ListAdapter(Context context, String[] imgNames, String[] imgUrls) { 
     super(context, R.layout.activity_main, imgNames); 
     this.imgNames = imgNames; 
     this.imgUrls = imgUrls; 
     this.context = context; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE); 
     View row = (View)inflater.inflate(R.layout.activity_main, parent, false); 

     TextView name = (TextView)row.findViewById(R.id.TextView); 
     new DownloadImageTask((ImageView) findViewById(R.id.ImageView)).execute(imgUrls[position]); 

     name.setText(imgNames[position]); 

     return row; 
    } 
} 

}

你有關於如何提高我的代碼的任何建議。我嘗試使用此代碼沒有列表視圖,它的工作,但不幸的是它不起作用的ListView。

+0

看看通用圖像加載器。你可以學習如何根據自己的源代碼自己做,或者只是使用該庫https://github.com/nostra13/Android-Universal-Image-Loader – dymmeh

回答

14

不要使用異步任務來執行此操作。使用經過良好測試的圖像加載器庫。

我建議https://github.com/koush/UrlImageViewHelper

我已經在三個高調的應用程序中使用這個庫,它是五十萬用戶的非常棒。

在getView方法

那麼做到這一點

UrlImageViewHelper.setUrlDrawable((ImageView) findViewById(R.id.ImageView), imgUrls[position]); 
+2

UrlImageViewHelper剛剛爲我節省了一大堆時間,謝謝提示 –

+4

不客氣,但現在,我正在推薦Square的Picasso。 –

+0

我現在就來看看。你能否給我快速的理由讓你決定改變畢加索? –

相關問題