【问题标题】:showing a progress bar or dialog from inside a custom adapter从自定义适配器内部显示进度条或对话框
【发布时间】:2014-04-24 11:07:17
【问题描述】:

我有一个带有图像的列表视图。用户可以点击缩略图,更大的图像将被下载并显示在新活动中的 ImageView 中。

onclick 在我的适配器中的 GetView 内:

holder.iconImage.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        try{

           //load the image
        String name = folderName.get(position).toString();
        Intent i = new Intent(context, ImageViewLarge.class);
        i.putExtra("link", link);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);


        }
        catch(Exception e){
            Toast.makeText(context, "errror: " + e.toString() , Toast.LENGTH_LONG).show();
        }
    }

});

显示图像可能需要几秒钟,所以我希望在加载图像时在屏幕上显示一个对话框或一个圆形进度旋转圆圈。

如果我的 onclick 在我的适配器内,我该怎么做?

我在适配器内部尝试过,但出现错误:

dialog = ProgressDialog.show(context, "Opening Image","Please wait..");

"Unable to add window — token null is not for an application” 

【问题讨论】:

  • 这应该是ImageViewLarge.class的责任。因为你正在下载我想的图像。

标签: android progress-bar progressdialog


【解决方案1】:

您可以使用此代码,它工作正常。

viewHolder.text.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            ProgressDialog dialog = new ProgressDialog(context);
            dialog.setMessage("Please wait.....");
            dialog.show();
            // you can add here  your stuffs
        }
    });

【讨论】:

  • 当我单击列表视图中的图像时仍然出现此错误:“android.view.WindowManager$BadTokenException: Unable to add window— token null is not for an application”
  • 它在我的项目中运行良好。我希望你的适配器没有得到正确的上下文使用活动上下文而不是应用程序上下文。
  • 这是我的适配器获取其上下文的方式: public ImageAdapter(Context c, ArrayList images, ArrayList folderName){ this.context = c;
  • 然后像这样调用它:adapter = new ImageAdapter(getApplicationContext(), pix, paths); lstView.setAdapter(适配器);
猜你喜欢
  • 2012-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-16
  • 1970-01-01
  • 2017-11-02
  • 2015-06-26
  • 1970-01-01
相关资源
最近更新 更多