2012-11-21 75 views
0

我目前正在使用SimpleAdapter在Android應用程序顯示圖像到我的列表視圖。我的圖像從我的在線服務器中檢索。我檢索圖像後,我試圖顯示圖像,但沒有顯示,也沒有錯誤。顯示圖像在listview中使用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創建drawable,然後查看是否有任何區別 –

+0

我還可以在哪裏保存圖像? –

+0

因爲你在try ... catch塊中包含了很多代碼,所以沒有錯誤。可能沒有圖像,因爲沒有JSON開始。你怎麼樣使用httpGet來獲取響應字符串,然後嘗試從它建立一個JSONObject。在任何情況下1)檢查有效的JSON格式的響應2)檢查解析的響應缺少JSON參數。編輯:3)並使用調試模式 – inmyth

回答

1

的問題是在這裏:

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

你設置關鍵像"[email protected]"一個字符串,然後將其綁定到R.id.list_image我以爲是一個ImageView的。這是行不通的。

我沒有使用可抽拉完全是這樣,但我有一個位圖的成功:

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

A SimpleAdapter沒有bindView方法,因此您可以提供一個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。抱歉。即時通訊新的Android和IM卡在這個問題數週。對於你的建議,我會試試看看它是否有幫助。謝謝 –

+0

我如何重寫bindView到我的simpleadapter? –

+0

我相信這適用於SimpleCursorAdapter而不適用於SimpleAdapter。我發現這個[link](https://developer.android.com/reference/android/widget/SimpleCursorAdapter.html),它顯示這些方法適用於'SimpleCursorAdapter'。 –

相關問題