我用一个简单的适配器创建了一个 json 解析来显示结果。一切正常,但我需要检索一个 url 以转换为位图。这是一个列表视图,因此 url 不是一个,而是一个、两个、三个或更多。对于每个 url,我需要创建一个位图,这样我就可以为列表的每个元素显示一个图像。实际上,在 getView() 方法部分之前,我可以在日志中正确显示 url。一旦我进入 getView() 我唯一能显示的 url 就是我从 json 解析的最后一个。这是onPostExecute()
我用来执行此操作的部分:
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
// Getting JSON Array from URL
rating = json.getJSONArray(TAG_ITEM);
for(int i = 0; i < rating.length(); i++)
{
JSONObject c = rating.getJSONObject(i);
// Storing JSON item in a Variable
String name = c.getString(TAG_NOME);
String commento = c.getString(TAG_COMMENTO);
String valore = c.getString(TAG_VALORE);
urlImage = c.getString(TAG_URLIMAGE);
String timestamp = c.getString(TAG_TIMESTAMP);
Log.i("Url immagine", urlImage);// here it shows 2 url and it's ok
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_NOME, name);
map.put(TAG_COMMENTO, commento);
map.put(TAG_VALORE, "Voted: "+valore);
map.put(TAG_TIMESTAMP, timestamp);
oslist.add(map);
ListAdapter adapter = new SimpleAdapter(RatingDetailsActivty.this, oslist, R.layout.list_rating_item,
new String[]
{ TAG_NOME,
TAG_COMMENTO,
TAG_VALORE,
TAG_URLIMAGE,
TAG_TIMESTAMP
}, new int[]
{
R.id.nome,
R.id.commento,
R.id.valore,
R.id.timestamp
})
{
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
ImageView profileImage = (ImageView) v.findViewById(R.id.profile_img);
try {
URL url = new URL(urlImage);
imageProfilo = BitmapFactory.decodeStream(url.openConnection().getInputStream());
Log.i("Url immagine", urlImage); // here there is only last one
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (imageProfilo != null) {
profileImage.setImageBitmap(imageProfilo);
} else {
profileImage.setImageDrawable(v.getResources() .getDrawable(R.drawable.welcome));
}
return v;
}
};
list.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
所以通过这种方式,imageview 只为列表的每个项目设置了一个图像(仅使用一个 url)。我在一个循环中..为什么不采用所有网址?