RecyclerView 适配器问题中的 ProgressDialog
ProgressDialog in RecyclerView adapter issue
当我尝试在 recyclerview 适配器中使用 progressdialog 时出现异常消息:
Unable to add window -- token null is not for an application
我知道使用上下文的一切都可以,因为 Glide ProgressBar 下面的几行使用相同的上下文。知道有什么问题吗?
我的适配器 class:
import android.app.ProgressDialog;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import java.util.List;
public class ShopsAdapter extends RecyclerView.Adapter<ShopsAdapter.MyViewHolder> {
Context mContext;
List<String> shopsList;
ProgressDialog progressDialog;
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView thumbnail;
public MyViewHolder(View view) {
super(view);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
}
}
public ShopsAdapter(Context mContext, List<String> shopsList) {
this.mContext = mContext;
this.shopsList = shopsList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.shop_card,parent,false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
progressDialog = new ProgressDialog(mContext);
shopsList.get(position);
try{
progressDialog.show();
String stringformat = String.format(".../%s.png", shopsList.get(position));
Glide.with(mContext).load(stringformat).listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
//progressDialog.setVisibility(View.GONE);
return false;
}
}).centerCrop().fitCenter().into(holder.thumbnail);
}catch (Exception e){
Log.d("Dsdfs", e.getMessage());
Glide.with(mContext).load("http://...Zabka.png").centerCrop().fitCenter().into(holder.thumbnail);
}
}
@Override
public int getItemCount() {
return shopsList.size();
}
}
改变
adapter = new ShopsAdapter(getApplicationContext(), shopsNames);
到
adapter = new ShopsAdapter(this, shopsNames);
当我尝试在 recyclerview 适配器中使用 progressdialog 时出现异常消息:
Unable to add window -- token null is not for an application
我知道使用上下文的一切都可以,因为 Glide ProgressBar 下面的几行使用相同的上下文。知道有什么问题吗? 我的适配器 class:
import android.app.ProgressDialog;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import java.util.List;
public class ShopsAdapter extends RecyclerView.Adapter<ShopsAdapter.MyViewHolder> {
Context mContext;
List<String> shopsList;
ProgressDialog progressDialog;
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView thumbnail;
public MyViewHolder(View view) {
super(view);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
}
}
public ShopsAdapter(Context mContext, List<String> shopsList) {
this.mContext = mContext;
this.shopsList = shopsList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.shop_card,parent,false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
progressDialog = new ProgressDialog(mContext);
shopsList.get(position);
try{
progressDialog.show();
String stringformat = String.format(".../%s.png", shopsList.get(position));
Glide.with(mContext).load(stringformat).listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
//progressDialog.setVisibility(View.GONE);
return false;
}
}).centerCrop().fitCenter().into(holder.thumbnail);
}catch (Exception e){
Log.d("Dsdfs", e.getMessage());
Glide.with(mContext).load("http://...Zabka.png").centerCrop().fitCenter().into(holder.thumbnail);
}
}
@Override
public int getItemCount() {
return shopsList.size();
}
}
改变
adapter = new ShopsAdapter(getApplicationContext(), shopsNames);
到
adapter = new ShopsAdapter(this, shopsNames);