2013-08-19 89 views
0

我嘗試將對話框中的圖像延遲加載圖像。從對話框外部獲取對話框內的視圖

我的問題是,我不能從我的對話框以外的對話框更新圖像的視圖。

我嘗試給每個視圖唯一的id,然後通過它的id找到視圖,但我得到空指針異常。

這裏是我的對話框代碼:

public void showAllComents() { 

     builderComment = new AlertDialog.Builder(this); 

     builderComment.setTitle(dietsList[poisition][0] + " comments"); 

     View prefView = View.inflate(this, R.layout.show_comments, null); 

     builderComment.setCancelable(false); 

     editComment = (EditText) prefView.findViewById(R.id.editComment); 

     LinearLayout commentsLinear = (LinearLayout) prefView.findViewById(R.id.commentsLinear); 

     if (haveComments) { 

      int length = comments.length; 

      for (int i = 0; i < length; i++) 
      {   
       LinearLayout tempLinear = new LinearLayout(this); 

       LayoutParams p1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 

       if (i != 0) 
       p1.setMargins(0, 20, 0, 0); 

       tempLinear.setOrientation(LinearLayout.HORIZONTAL); 
       tempLinear.setLayoutParams(p1); 

       ImageView image = new ImageView(this); 
       p1 = new LayoutParams(64, 64); 
       image.setLayoutParams(p1); 
       image.setId(6000+i); 
       image.setImageDrawable(getResources().getDrawable(R.drawable.anonymous)); 

       tempLinear.addView(image); 

       commentsLinear.addView(tempLinear); 
      } 

... 
... 
... \\\Rest of dialog code... 

我打開我的圖像,然後我嘗試更新imageviews。

public void setCommentedUsersProfilePictrue() 
     { 
      int lengthUnique = commentsuniqueNames.length; 
      int loadLength = comments.length; 

      View prefView = View.inflate(this, R.layout.show_comments, null); 

      for(int i = 0; i < loadLength; i++) 
      { 
       for(int j = 0; j < lengthUnique; j++) 
       { 
        if(comments[i][1].equals(commentsuniqueNames[j])) 
        { 
         Log.i("user name comment", comments[i][1]); 
         //Log.i("user name", userProfilePictures[i][1]); 

         ImageView tempImage = new ImageView(DietWall.this); 

         tempImage = (ImageView) prefView.findViewById(6000 + i); 

         if(tempImage != null && commentsLoadedPictrues[j][1] != null && commentsLoadedPictrues[j][1].equals("null") == false && commentsLoadedPictrues[j][1].equals("NULL") == false) 
          tempImage.setImageDrawable(new BitmapDrawable(getResources(),decodeBase64(commentsLoadedPictrues[j][1]))); 
         else 
          Log.i("null", "null"); 
        } 
        } 
       } 
       } 
+0

在這裏發表您的logcat .. –

+0

我的日誌是(「空」,「空」) – dasdasd

回答

1

你需要你的對話框的觀點,你會發現你的ImageView像這樣:

View ImageView image = dialog.findViewById(R.id.image); 

但我建議使用下列庫,它負責圖像加載/緩存以及更多的,它只是使你的生活更輕鬆;-)

https://code.google.com/p/android-query/#Image_Loading

//fetch and set the image from internet, cache with file and memory 
aq.id(R.id.image1).image("http://www.vikispot.com/z/images/vikispot/android-w.png"); 

個特點:

  • 簡單
  • 內存&文件緩存
  • 下采樣
  • 可縮放(的WebView)
  • 備份圖片
  • 預加載
  • 動畫
  • 動態寬高比
  • 避免重複同時獲取
  • 自定義回調
+0

它的工作,謝謝! – dasdasd