2012-03-15 133 views
0

我試圖在列表中設置drawable。我的列表不包含圖像。 我使用幾個TextViews和一個ImageView從list_item.xml中填充它。異步從互聯網/數據庫下載圖像到Android ListView

如果我的圖像存儲在Res android文件夾中,我沒有任何問題,但是我在這裏從數據庫加載此圖像。

現在,我用這個方法王的:

SimpleAdapter adapter = new SimpleAdapter(this.getBaseContext(), list, 
R.layout.xml_leaderboard_item, keys, views); 

有沒有辦法在我的列表中添加該繪製/位圖?或者可能以編程方式將該圖像放在Res文件夾中,以便我可以使用它?

回答

0

我終於能夠做到我想要的。下面是結果:

enter image description here

我從我的數據庫異步下載這些圖片。我無法將其添加到ListView中... 以下是我可能感興趣的代碼。

public class LeaderboardScreen extends ListActivity implements View.OnClickListener 
{ 
    private UserAdapter    adapter; 
    private ArrayList<User>   users; 
    private UserPictureDownloader user_picture_cache; 
    ... 

    protected void onCreate(Bundle saved_instance_state) 
    { 
     ... 
     users = new ArrayList<User>(); 
     adapter = new UserAdapter(this, R.layout.xml_leaderboard_item, users); 
     user_picture_cache = new UserPictureDownloader(); 
     setListAdapter(adapter);  
     ... 
    }  


    private class UserAdapter extends ArrayAdapter<User> 
    { 
     private ArrayList<User>  users; 

     public UserAdapter(Context context, int text_view_res_id, ArrayList<User> users) 
     { 
      super(context, text_view_res_id, users); 
      this.users = users; 
     } 

     public View   getView(int position, View view, ViewGroup parent) 
     { 
      LayoutInflater layout = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = layout.inflate(R.layout.xml_leaderboard_item, null); 


      ImageView iv_pic = (ImageView)view.findViewById(R.id.lead_pic); 
      TextView et_position = (TextView)view.findViewById(R.id.lead_position); 
      TextView et_user = (TextView)view.findViewById(R.id.lead_user); 
      TextView et_score = (TextView)view.findViewById(R.id.lead_s_value); 
      TextView et_reput = (TextView)view.findViewById(R.id.lead_r_value); 
      TextView et_contrib = (TextView)view.findViewById(R.id.lead_c_value); 
      TextView et_bronze = (TextView)view.findViewById(R.id.lead_bronze_value); 
      TextView et_silver = (TextView)view.findViewById(R.id.lead_silver_value); 
      TextView et_gold = (TextView)view.findViewById(R.id.lead_gold_value); 

      User current_user = users.get(position); 
      if (current_user != null) 
      {    
       // Set available informations right away. 
       et_position.setText(String.valueOf(position + 1)); 
       et_user.setText(current_user.GetLogin()); 
      et_score.setText(String.valueOf(current_user.GetScore())); 
      et_reput.setText(String.valueOf(current_user.GetReputation())); 
      et_contrib.setText(String.valueOf(current_user.GetNumberOfQuestionPost())); 
      et_bronze.setText(String.valueOf(current_user.GetBadgeBronze())); 
      et_silver.setText(String.valueOf(current_user.GetBadgeSilver())); 
      et_gold.setText(String.valueOf(current_user.GetBadgeGold())); 

      // Fetch user image asynchronously. 
      final int pos = position; 
      final String user_login = current_user.GetLogin(); 
      final ImageView _iv_pic = iv_pic; 

      final Handler handler = new Handler() 
       { 
       @Override 
       public void   handleMessage(Message message) 
       { 
        _iv_pic.setImageDrawable((Drawable)message.obj); 
       } 
      }; 

      Thread thread = new Thread() 
      { 
       @Override 
       public void   run() 
       { 
        try 
        { 
         Drawable dwb = (Drawable)user_picture_cache.GetCachedObject(user_login, handler); 
         handler.sendMessage(handler.obtainMessage(0, dwb)); 
        } 
        catch (Exception e) { Log.e(TAG, e.getMessage()); } 
       } 
      }; 
      thread.start(); 
     } 
     return view; 
    } 
} 

的DownloadCache類:

public class DownloadCache 
{ 
    private final String TAG = "DownloadCache"; 

    private ConcurrentHashMap<String, CountDownLatch> queued_downloads; 
    private ConcurrentHashMap<String, Object>   cache; 

    public DownloadCache() 
    { 
     queued_downloads = new ConcurrentHashMap<String, CountDownLatch>(); 
     cache = new ConcurrentHashMap<String, Object>(); 
    } 


    public Object GetCachedObject(String url, Handler handler) throws InterruptedException 
    { 
     // Check for a running download, if found wait for its result. 
     CountDownLatch complete_event = queued_downloads.get(url); 
     if (complete_event != null) 
     complete_event.await(); 

     // Check for cached object. 
     Object obj = cache.get(url); 
     if (obj != null) 
      return obj; 

     obj = StartDownload(url, handler); 
     return obj; 
    } 

private Object StartDownload(String url, Handler handler) 
    { 
     CountDownLatch complete_event = new CountDownLatch(1); 
     queued_downloads.put(url, complete_event); 

     Object obj = DoDownloadWork(url, handler); 
     cache.put(url, obj); 

     queued_downloads.remove(url); 

     complete_event.countDown(); 
     return obj; 
    } 

    public Object DoDownloadWork(String url, Handler handler) 
    { 
     return null; 
    } 
} 

希望這有助於。

1

使用BitmapFactory類對數據庫中的圖像字節數據進行解碼。然後在您的ImageView上使用setImageBitmapsetImageDrawable方法。

byte[] imageData = ...;  
Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length); 
imageView.setImageBitmap(bitmap); 
+0

我不想讓我的數據庫圖像在drawable中,我已經做到了。我試圖把這個drawable放在一個ListView中,其中每個條目都來自list_item.xml。在這個XML中,我有幾個TextView和一個ImageView(我想要Picure中的一個)。 – 2012-03-15 11:46:01

+0

我更新了我的答案。 – xaviert 2012-03-15 12:27:10

相關問題