2012-11-19 56 views
1

我有一個小問題,使用簡單的適配器將圖像放入列表視圖。我從我的在線服務器(AMAZON)獲取圖像。基於用戶ID下載圖像後,我嘗試將它們設置在我的列表視圖中,但沒有顯示,也沒有發生錯誤。使用simpleAdapter與圖像的列表視圖

下面是我的代碼:

// looping through All applicants 
       for (int i = 0; i < applicant.length(); i++) { 
        JSONObject c = applicant.getJSONObject(i); 

        // Storing each JSON item in variable 
        String uid = c.getString(TAG_UID); 
        String name = c.getString(TAG_NAME); 
        String overall = c.getString(TAG_OVERALL); 
        String apply_datetime = c.getString(TAG_APPLY_DATETIME); 
        String photo = c.getString(TAG_PHOTO); 

        // creating new HashMap 
        //HashMap<String, String> map = new HashMap<String, String>(); 

        //IMAGE 
        HashMap<String, Object> map = new HashMap<String, Object>(); 

        // adding each child node to HashMap key (value) 
        map.put(TAG_UID, uid); 
        map.put(TAG_NAME, name); 
        map.put(TAG_OVERALL, overall); 
        map.put(TAG_APPLY_DATETIME, apply_datetime); 

        // adding HashList to ArrayList 
        // applicantsList.add(map); 

        // LISTING IMAGE TO LISTVIEW 
        try { 
         imageURL = c.getString(TAG_PHOTO); 

         InputStream is = (InputStream) new URL(
           "my url link/images/" 
             + imageURL).getContent(); 
         d = Drawable.createFromStream(is, "src name"); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 

        map.put(TAG_PHOTO, d); 

        // adding HashList to ArrayList 
        applicantsList.add(map); 
       } 

正如你可以看到,在我下載的圖像。我設置爲listview使用simpleAdapter下面:

SimpleAdapter adapter = new SimpleAdapter(
          SignUpApplicantActivity.this, applicantsList, 
          R.layout.list_applicant, new String[] { 
            TAG_UID, TAG_NAME, TAG_OVERALL, 
            TAG_APPLY_DATETIME, TAG_PHOTO }, new int[] { 
            R.id.applicantUid, R.id.applicantName, 
            R.id.applicantOverall, 
            R.id.apply_datetime, R.id.list_image }); 
        // updating listView 
        setListAdapter(adapter); 
+0

你可以檢查創建'Drawable's是空或不是? (例如有斷點還是日誌跟蹤)? – fiddler

+0

@fiddler Drawable不爲null。 –

+0

不要將標籤(如「Android」)添加到主題中,也不要添加不相關的標籤(如「Eclipse」)。 –

回答

1

你叫notifyDatasetChange()?如果您不調用適配器,您的適配器可能不會失效。

+0

我didnt調用notifyDatasetChange()。我不確定我使用的方法是否正確。 –

+0

剛剛在 setListAdapter(adapter); 電話: adapter.notifyDataSetChanged(); 它可能會伎倆 – Ercan

+0

好的。我試試看。 –

1

SimpleAdapter documentation的圖像數據被預期爲資源ID或字符串(圖像URI) - 見setViewImage(ImageView,String)

我看到2級的解決方案:

  1. 在數據映射提供一個URI ,不是可繪製的。
  2. 實現自己的觀點粘合劑可繪結合的ImageView:

    adapter.setViewBinder(new SimpleAdapter.ViewBinder() { 
    
        @Override 
        public boolean setViewValue(View view, Object data, String textRepresentation) { 
         if(view.getId() == R.id.list_image) { 
          ImageView imageView = (ImageView) view; 
          Drawable drawable = (Drawable) data; 
          imageView.setImageDrawable(drawable); 
          return true; 
         } 
         return false; 
        } 
    }); 
    
+0

好的。從我的代碼我如何從drawable更改爲uri?你能告訴我一個例子嗎? –

+0

看到我的第二個建議 – fiddler

+0

okok我試試看,如果我有任何問題,請再次諮詢。謝謝! –

0

嘗試

  try{ 
      for (int i = 0; i < applicant.length(); i++) { 
       JSONObject c = applicant.getJSONObject(i); 

       // Storing each JSON item in variable 
       String uid = c.getString(TAG_UID); 
       String name = c.getString(TAG_NAME); 
       String overall = c.getString(TAG_OVERALL); 
       String apply_datetime = c.getString(TAG_APPLY_DATETIME); 
       String photo = c.getString(TAG_PHOTO); 

       // creating new HashMap 
       HashMap<String, String> map = new HashMap<String, String>(); 

       // adding each child node to HashMap key (value) 
       map.put(TAG_UID, uid); 
       map.put(TAG_NAME, name); 
       map.put(TAG_OVERALL, overall); 
       map.put(TAG_APPLY_DATETIME, apply_datetime); 
       map.put(TAG_PHOTO, photo); 

       // adding HashList to ArrayList 
       applicantsList.add(map); 
      } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      return null; 
     } 
      SimpleAdapter adapter = new SimpleAdapter(
         SignUpApplicantActivity.this, applicantsList, 
         R.layout.list_applicant, new String[] { 
           TAG_UID, TAG_NAME, TAG_OVERALL, 
           TAG_APPLY_DATETIME, TAG_PHOTO }, new int[] { 
           R.id.applicantUid, R.id.applicantName, 
           R.id.applicantOverall, 
           R.id.apply_datetime, R.id.list_image }); 
       // updating listView 
       setListAdapter(adapter); 



也許我的代碼可以幫助你!?! 我已

convertDrawable(INT整數)

和將String.valueOf(convertDrawable(iconId))

HashF.put(TAG_ID, String.valueOf(convertDrawable(iconId))); 


private void getList(JSONObject json) { 
     try { 
      details = json.getJSONArray(TAG_LIST); 

      for (int i = 0; i < details.length(); i++) { 

       JSONObject main = details.getJSONObject(i); 
       Integer date = main.getInt(TAG_DATE); 
       JSONArray nArray = main.getJSONArray(TAG_NOW); 

       JSONObject detail = nArray.getJSONObject(0); 
       Integer iconId = detail.getInt(TAG_ID); 

       HashMap<String, String> HashF = new HashMap<String, String>(); 

       HashF.put(TAG_DATE, convertDate(date)); 
       HashF.put(TAG_ID, String.valueOf(convertDrawable(iconId))); 

       fList.add(HashF); 
      } 
     } catch (Exception e) { 

     } 
     ListAdapter adapter = 
       new SimpleAdapter(getActivity(), 
         fList, R.layout.list_item, 
       new String[]{ 
         TAG_DATE, 
         TAG_ID 
       }, 
       new int[]{ 
         R.id.datum, 
         R.id.icon_fore 
       }); 

     setListAdapter(adapter); 
    } 

    public static String convertDate(Integer Time) { 
     SimpleDateFormat sdf = 
       new SimpleDateFormat(
         "dd, MMMM", Locale.getDefault()); 

     Calendar kal = 
       Calendar.getInstance(); 

     kal.setTimeInMillis(Time * 1000L); 
     sdf.setTimeZone(kal.getTimeZone()); 
     return sdf.format(kal.getTime()); 

    } 


    public static int convertDrawable(int aId){ 
     int icon = 0; 

     if(aId == 8300){ 
      icon = R.drawable.draw1; 
     } 
     if (aId >= 7010 && actualId < 7919){ 
      icon = R.drawable.draw2; 
     } 
     if (aId >= 2010 && actualId <3010) { 
      icon = R.drawable.draw3; 
     } 
     if (aId >= 3010 && actualId < 4010) { 
      icon = R.drawable.draw4; 
     } 
     if (aId >= 6010 && actualId < 7010) { 
      icon = R.drawable.draw5; 
     } 
     if (aId >= 5010 && actualId < 6010) { 
      icon = R.drawable.draw6; 
     } 
     if (aId == 3801){ 
      icon = R.drawable.draw7; 
     } 
     if (aId == 8032){ 
      icon = R.drawable.draw8; 
     } 
     if (aId == 8083){ 
      icon = R.drawable.draw9; 
     } 
     if (aId == 8704) { 
      icon = R.drawable.draw10; 
     } 

     return icon; 
    } 


反正讓看看Bitmap is not a Drawable