2012-11-21 2 views
0

현재 SimpleAdapter를 사용하여 이미지를 내 목록보기에 표시하는 Android 애플리케이션을 개발 중입니다. 내 이미지는 내 온라인 서버에서 검색됩니다. 이미지를 검색 한 후에 이미지를 표시하려고했지만 아무 것도 표시되지 않고 오류도 표시되지 않았습니다. 다음은 simpleAdapter ANDROID를 사용하여 목록보기에 이미지 표시

내 코드

class ListApplicant extends AsyncTask<String, String, String> { 

    // Before starting background thread Show Progress Dialog 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(SignUpApplicantActivity.this); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(false); 
    } 

    // getting All applicants from URL 
    protected String doInBackground(String... args) { 
     // Building Parameters 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("eid", eid)); 

     // getting JSON string from URL 
     JSONObject json = jParser.makeHttpRequest(url_list_applicant, 
       "GET", params); 

     // Check your log cat for JSON response 
     Log.d("All applicants: ", json.toString()); 

     try { 
      // Checking for SUCCESS TAG 
      int success = json.getInt(TAG_SUCCESS); 

      if (success == 1) { 
       // applicants found 
       // Getting Array of applicants 
       applicant = json.getJSONArray(TAG_APPLICANTS); 

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

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

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

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

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

         InputStream is = (InputStream) new URL(
           "http://ec2-175-41-164-218.ap-southeast-1.compute.amazonaws.com/android/images/" 
             + imageURL).getContent(); 
         d = Drawable.createFromStream(is, "src name"); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 

        map.put(TAG_PHOTO, d.toString()); 

        // adding HashList to ArrayList 
        applicantsList.add(map); 
       } 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    // After completing background task Dismiss the progress dialog 
    protected void onPostExecute(String file_url) { 
     // dismiss the dialog after getting all applicants 
     pDialog.dismiss(); 
     // updating UI from Background Thread 
     runOnUiThread(new Runnable() { 
      public void run() { 
       if (applicantsList.isEmpty()) { 
        applicantDisplay 
          .setText("No applicants have signed up yet"); 
       } else { 

        //Updating parsed JSON data into ListView 

        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

InputStream에서 드로어 블을 직접 생성하는 대신 이미지를 먼저 저장해보고 어떤 차이가 있는지 확인하십시오. –

+0

이미지를 저장할 수있는 곳은 어디입니까? –

+0

try ... catch 블록에 많은 코드를 래핑했기 때문에 오류가 발생하지 않았습니다. JSON이 없기 때문에 아마도 이미지가 없었을 것입니다. HTTPGet을 사용하여 응답 문자열을 얻은 다음 JSONObject를 작성해보십시오. 어떤 경우 든 1) 유효한 JSON 형식에 대한 응답을 확인하십시오. 2) 구문 분석 된 응답에서 누락 된 JSON 매개 변수를 확인하십시오. 편집 : 3) 그리고 디버그 모드를 사용 – inmyth

답변

1

문제는 여기에 있습니다 :

map.put(TAG_PHOTO, d.toString()); 

당신은 내가 가정 R.id.list_image에 바인딩 할 "[email protected]" 같은 문자열에 해당 키를 설정하여 이미지 뷰입니다 . 그것은 작동하지 않습니다.

꽤 그런 드로어 블을 사용하지 않은,하지만 난 비트 맵 성공 있었 : 다음

Bitmap bitmap = BitmapFactory.decodeStream(is); 

그리고 이미지 설정 내 어댑터에 bindView를 오버라이드 :

public void bindView(View view, Context context, Cursor cursor) { 
    // do the default stuff 
    super.bindView(view, context, cursor); 

    // now set the image 
    ImageView imgView = (ImageView) view.findViewById(R.id.imageView1); 
    imgView.setImageBitmap(bitmap); 
} 

SimpleAdapterbindView 방법이 없으므로 대신 당신은 ViewBinder 제공 할 수

mySimpleAdapter.setViewBinder(new SimpleAdapter.ViewBinder() { 
    @Override 
    public boolean setViewValue(View view, Object data, String textRepresentation) { 
     if (view.getId().equals(R.id.my_img_view_id)) { 
      ((ImageView) view).setImageBitmap(bitmap); 

      // we have successfully bound this view 
      return true; 
     } else { 
      // allow default binding to occur 
      return false; 
     } 
    } 
}); 
+0

oohh. 죄송합니다. 메신저에 안드로이드와 인스턴트 메신저이 문제에 몇 주 동안 붙어 있습니다. 당신의 제안에 도움이되는지 확인하려고 노력할 것입니다. 감사합니다 –

+0

어떻게 내 simpleadapter에 bindView를 재정의합니까? –

+0

나는 이것이 'SimpleAdapter'가 아니라'SimpleCursorAdapter'에서 작동한다고 생각합니다. 나는이 메소드가 'SimpleCursorAdapter'에서 작동 함을 보여주는 [link] (https://developer.android.com/reference/android/widget/SimpleCursorAdapter.html)을 발견했습니다. –

관련 문제