0

我正在使用HashMap通過simpleAdapter在列表視圖中顯示圖像和文本。現在這適用於我的R.drawable圖像。但是,只要我從遠程源獲取圖像作爲位圖,它就不會顯示圖像。正在正確下載圖像,並且在圖像視圖中顯示圖像時顯示正常。這裏是我的檢索圖像並將其存儲在一個位圖變量代碼:列表視圖不顯示遠程圖像

user_picture=(ImageView)findViewById(R.id.widget89); 
    java.net.URL img_value = null; 
    try { 
     img_value = new java.net.URL("http://graph.facebook.com/XXXXXXX/picture?type=small"); 
    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    try { 
     mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream()); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    user_picture.setImageBitmap(mIcon1); 

任何想法,爲什麼發生這種情況?

預先感謝 Jannik

回答

0

在最近的項目中,我使用的以下類用於獲取位圖和在自定義對象存儲這些和使用這些用於使用適配器填充一個ListView:

package com.octoshape.android.octopocplayer.playlist; 

import java.io.BufferedInputStream; 
import java.io.IOException; 
import java.net.URL; 
import java.net.URLConnection; 

import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.AsyncTask; 

public class BitmapDownloader extends AsyncTask<String, Void, Bitmap>{ 

    @Override 
    protected Bitmap doInBackground(String... urls) { 
     try { 
      URL url = new URL(urls[0]); 
      URLConnection conn = url.openConnection(); 
      conn.connect(); 
      BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); 
      Bitmap bm = BitmapFactory.decodeStream(bis); 
      bis.close(); 
      return bm; 
     } catch (IOException e) {} 
      return null; 
    } 
} 

這裏是適配器:

private class VideoAdapter extends BaseAdapter { 

     @Override 
     public int getCount() { 
      return playList.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return playList.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public int getViewTypeCount() { 
      return ITEM_VIEW_TYPE_COUNT; 
     } 

     @Override 
     public int getItemViewType(int position) { 
      return (playList.get(position) instanceof String) ? ITEM_VIEW_TYPE_CHANNELSET 
        : ITEM_VIEW_TYPE_CHANNEL; 
     } 

     @Override 
     public boolean isEnabled(int position) { 
      // A separator cannot be clicked ! 
      return getItemViewType(position) != ITEM_VIEW_TYPE_CHANNELSET; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 

      final int type = getItemViewType(position); 

      // First, let's create a new convertView if needed. You can also 
      // create a ViewHolder to speed up changes if you want ;) 
      if (convertView == null) { 
       final LayoutInflater inflater = LayoutInflater 
         .from(DemoPlayer.this); 
       final int layoutID = type == ITEM_VIEW_TYPE_CHANNELSET ? R.layout.separator_list_item 
         : R.layout.video_list_item; 
       convertView = inflater.inflate(layoutID, parent, false); 
      } 

      // We can now fill the list item view with the appropriate data. 
      if (type == ITEM_VIEW_TYPE_CHANNELSET) { 
       ((TextView) convertView).setText((String) getItem(position)); 
      } else { 
       final Channel channel = (Channel) getItem(position); 
       ((TextView) convertView.findViewById(R.id.name)) 
         .setText(channel.getName()); 
       ((ImageView) convertView.findViewById(R.id.logo)) 
         .setImageResource(R.drawable.default); 

       if (channel.getChannelImage() != null) 
        ((ImageView) convertView.findViewById(R.id.channellogo)) 
          .setImageBitmap(channel.getChannelImage()); 
      } 
      return convertView; 
     } 
    }