-1

我用一个简单的适配器创建了一个 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)。我在一个循环中..为什么不采用所有网址?

4

3 回答 3

1

试试这个:在 onPostExecute 创建你的地图列表

@Override
protected void onPostExecute(JSONObject json) {

    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);
            String imageUrl = c.getString(TAG_URLIMAGE);
            String timestamp = c.getString(TAG_TIMESTAMP);
            Log.i("Url immagine", imageUrl);// 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_IMAGEURL, imageUrl);
            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_IMAGEURL,
                                    TAG_TIMESTAMP
                            }, new int[]
                    {
                            R.id.nome,
                            R.id.commento,
                            R.id.valore,
                            R.id.timestamp
                    });
        }
        list.setAdapter(adapter);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

在 getView 中,使用 getItem(position) 检索您的列表项,从中检索您的 url

@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 {
    HashMap<String, String> map = (HashMap<String, String>)getItem(position);
    URL url = new URL(map.get(TAG_IMAGEURL));
    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;

}

于 2014-11-28T16:54:01.737 回答
0

在您的代码中,您使用局部变量 (urlImage),您需要将您的 URL 放入您的数据中并从getItem(int position) ( http://developer.android.com/reference/android/widget/SimpleAdapter ) 获取 URL .html#getItem(int) )

URL url = 新 URL(urlImage);

于 2014-11-27T14:14:40.090 回答
0

您似乎在 getView() 之外缓存您的视图,而不是使用传递给您的 convertView 。

Romain Guy 在这里解释它https://www.youtube.com/watch?v=wDBM6wVEO70

于 2014-11-27T14:19:18.427 回答