我終於能夠做到我想要的。下面是結果:
我從我的數據庫異步下載這些圖片。我無法將其添加到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;
}
}
希望這有助於。
我不想讓我的數據庫圖像在drawable中,我已經做到了。我試圖把這個drawable放在一個ListView中,其中每個條目都來自list_item.xml。在這個XML中,我有幾個TextView和一個ImageView(我想要Picure中的一個)。 – 2012-03-15 11:46:01
我更新了我的答案。 – xaviert 2012-03-15 12:27:10