从对话框外部获取对话框内的视图

时间:2013-08-19 13:42:51

标签: android android-dialog

我尝试将图像延迟加载到对话框内的图像。

我的问题是我无法从对话框外的对话框中获取视图以更新图像。

我尝试给每个视图唯一的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...

我加载了图片,然后尝试更新图片浏览。

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

1 个答案:

答案 0 :(得分: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"); 

特点:

  • 简单
  • 记忆&amp;文件缓存
  • Down Sampling
  • Zoomable(WebView)
  • 后备图片
  • 预压
  • 动画
  • 动态宽高比
  • 避免重复同时提取
  • 自定义回拨