2012-11-19 5 views
1

J'ai un petit problème avec mettre une image à une vue de liste en utilisant un adaptateur simple. Je reçois l'image de mon serveur en ligne (AMAZON). Après avoir téléchargé l'image en fonction de l'identifiant de l'utilisateur, j'essaie de les définir dans ma listview mais rien n'a été affiché et aucune erreur n'est survenue.Utilisation de simpleAdapter avec image pour listview

Ci-dessous mon code:

// 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); 
       } 

Comme vous pouvez le voir, après le téléchargement de l'image. J'ai mis à listview en utilisant simpleAdapter ci-dessous:

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

Pouvez-vous vérifier si les créés 'Drawable's sont nuls ou non? (par exemple avec un point d'arrêt ou une trace de journal)? – fiddler

+0

@fiddler Drawable n'est pas nul. –

+0

n'ajoutez pas de tags (comme "Android") à l'objet et n'ajoutez pas de tags non pertinents (comme "Eclipse"). –

Répondre

1

Avez-vous appelé notifyDatasetChange()? votre carte ne peut pas être invalidée si vous ne l'appelez pas.

+0

je n'ai pas appelé notifyDatasetChange(). Je ne suis pas sûr que la méthode utilisée soit correcte. –

+0

juste après setListAdapter (adaptateur); appelez: adapter.notifyDataSetChanged(); Il peut faire l'affaire – Ercan

+0

ok. Je l'essaie. –

1

De l'SimpleAdapter documentation les données d'image devrait être un ID de ressource ou une chaîne (une image URI) - voir setViewImage(ImageView,String)

Je vois 2 solutions:

  1. Fournir un URI dans la carte de données , pas un drawable.
  2. Mettre en oeuvre votre propre liant en vue de lier le dessinable du 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

ok. de mon code comment puis-je passer de drawable à uri? Pouvez-vous m'en montrer un exemple? –

+0

Voir ma 2ème suggestion – fiddler

+0

okok je l'essaie et vous consulte à nouveau si j'avais un problème. Merci! –

0

Essayez

  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); 



Peut-être que mon code peut vous aider!?! J'ai fait

convertDrawable (int entier)

et 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; 
    } 


Quoi qu'il en soit faire un coup d'œil à Bitmap is not a Drawable

Questions connexes