Android 无法在非活动的RecyclerView适配器类中显示对话框
我在我的Android 无法在非活动的RecyclerView适配器类中显示对话框,android,android-fragments,android-recyclerview,android-context,android-dialog,Android,Android Fragments,Android Recyclerview,Android Context,Android Dialog,我在我的片段中使用Recyclerview。我的Recyclerview有Adapter类。我需要用我在AdapterClass中编写的一种方法显示AlertDialog。但我得到了一个错误 final AlertDialog alertDialog=new AlertDialog.Builder(context).create(); final AlertDialog.Builder builder = new AlertDialog.Builder(context); android.ap
片段中使用Recyclerview
。我的Recyclerview有Adapter
类。我需要用我在AdapterClass
中编写的一种方法显示AlertDialog
。但我得到了一个错误
final AlertDialog alertDialog=new AlertDialog.Builder(context).create();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
android.app.Activity无法应用于android.content.Context
final AlertDialog alertDialog=new AlertDialog.Builder(context).create();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
这是我的AlertClass
public class AlertClass {
public void noInternetAlert(Activity activity)
{
final AlertDialog alertDialog=new AlertDialog.Builder(activity).create();
AlertDialog.Builder builder=new AlertDialog.Builder(activity);
builder.setTitle("No Internet Connection");
builder.setMessage("You need to have Mobile data or Wifi to access this.");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
alertDialog.cancel();
}
});
builder.show();
}
}
private void free() {
AlertClass alert=new AlertClass();
alert.noInternetAlert(context);
}
final AlertDialog alertDialog=new AlertDialog.Builder(context).create();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
这是我在AdapterClass
public class AlertClass {
public void noInternetAlert(Activity activity)
{
final AlertDialog alertDialog=new AlertDialog.Builder(activity).create();
AlertDialog.Builder builder=new AlertDialog.Builder(activity);
builder.setTitle("No Internet Connection");
builder.setMessage("You need to have Mobile data or Wifi to access this.");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
alertDialog.cancel();
}
});
builder.show();
}
}
private void free() {
AlertClass alert=new AlertClass();
alert.noInternetAlert(context);
}
final AlertDialog alertDialog=new AlertDialog.Builder(context).create();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
这是适配器类的代码
public class BusyAdapter extends RecyclerView.Adapter<BusyAdapter.ViewHolder>
{
private FragmentManager fm;
private FragmentTransaction tx;
private String myToken,name,join,description,id;
private ArrayList<BusyEntry> entry;
Context context;
public BusyAdapter(ArrayList<BusyEntry> entry,Context context)
{
this.entry = entry;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.busy_card,parent,false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final BusyEntry currentEntry=entry.get(position);
holder.name.setText(currentEntry.getName());
holder.description.setText(currentEntry.getDescription());
holder.id.setText(currentEntry.getId());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int pos=holder.getAdapterPosition();
name=entry.get(position).getName();
join=entry.get(position).getJoin();
description=entry.get(position).getDescription();
id=entry.get(position).getId();
final AppCompatActivity activity=(AppCompatActivity)v.getContext();
final AlertDialog alertDialog=new AlertDialog.Builder(activity).create();
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Confirmation");
builder.setMessage("Do you really want to Free?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
free();
String chooseTab="";
ChooseTab chooseTab1=new ChooseTab();
fm=activity.getSupportFragmentManager();
Busy busy=new Busy();
tx=fm.beginTransaction();
tx.replace(R.id.frame,chooseTab1,chooseTab);
tx.detach(myentry);
tx.attach(myebtry);
entry.remove(pos);
notifyItemRemoved(pos);
tx.commit();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
alertDialog.cancel();
}
});
builder.show();
}
});
}
final AlertDialog alertDialog=new AlertDialog.Builder(context).create();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
公共类BusyAdapter扩展了RecyclerView.Adapter
{
私人碎片管理器fm;
私人碎片交易;
私有字符串myToken、名称、连接、描述、id;
私人ArrayList条目;
语境;
公共总线适配器(ArrayList条目、上下文)
{
this.entry=entry;
this.context=上下文;
}
@凌驾
public ViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图v=LayoutInflater.from(parent.getContext()).flate(R.layout.busy_卡,parent,false);
返回新的视图持有者(v);
}
@凌驾
公共无效onBindViewHolder(最终视图持有人,最终整型位置){
最终母线入口电流入口=入口。获取(位置);
holder.name.setText(currentEntry.getName());
holder.description.setText(currentEntry.getDescription());
holder.id.setText(currentEntry.getId());
holder.itemView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
final int pos=holder.getAdapterPosition();
name=entry.get(position.getName();
join=entry.get(position.getJoin();
description=entry.get(position.getDescription();
id=entry.get(position.getId();
最终的AppCompatActivity活动=(AppCompatActivity)v.getContext();
final AlertDialog AlertDialog=新建AlertDialog.Builder(活动).create();
最终AlertDialog.Builder=新建AlertDialog.Builder(活动);
建造商名称(“确认书”);
setMessage(“你真的想释放吗?”);
builder.setPositiveButton(“是”,新建DialogInterface.OnClickListener()
{
@凌驾
public void onClick(DialogInterface dialog,int which)
{
自由();
字符串chooseTab=“”;
ChooseTab chooseTab1=新的ChooseTab();
fm=activity.getSupportFragmentManager();
Busy Busy=新忙();
tx=fm.beginTransaction();
tx.replace(R.id.frame,chooseTab1,chooseTab);
tx.detach(myentry);
附件(myebtry);
进入。移除(pos);
已删除的项目(pos);
tx.commit();
}
});
setNegativeButton(“否”,新的DialogInterface.OnClickListener()
{
@凌驾
public void onClick(DialogInterface dialog,int which){
alertDialog.cancel();
}
});
builder.show();
}
});
}
如何在此方法中显示对话框?只需使用v.getContext像这样更改AlertDialog.Builder AlertDialog=new AlertDialog.Builder(v.getContext);
final AlertDialog alertDialog=new AlertDialog.Builder(context).create();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
只需使用v.getContext像这样更改AlertDialog.Builder AlertDialog=new AlertDialog.Builder(v.getContext);
final AlertDialog alertDialog=new AlertDialog.Builder(context).create();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
在AlertDialog.Builder
public void noInternetAlert(Activity activity)
{
Context context = activity;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
....
}
final AlertDialog alertDialog=new AlertDialog.Builder(context).create();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
在AlertDialog.Builder
public void noInternetAlert(Activity activity)
{
Context context = activity;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
....
}
final AlertDialog alertDialog=new AlertDialog.Builder(context).create();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
适配器类中已经有上下文。需要使用上下文创建alertDialog
final AlertDialog alertDialog=new AlertDialog.Builder(context).create();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
先拆下这一行
final AppCompatActivity activity=(AppCompatActivity)v.getContext();
final AlertDialog alertDialog=new AlertDialog.Builder(context).create();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
然后在这里使用上下文
final AlertDialog alertDialog=new AlertDialog.Builder(context).create();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
希望这会有所帮助。您在适配器类中已经有了上下文。您需要使用上下文来创建alertDialog
final AlertDialog alertDialog=new AlertDialog.Builder(context).create();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
先拆下这一行
final AppCompatActivity activity=(AppCompatActivity)v.getContext();
final AlertDialog alertDialog=new AlertDialog.Builder(context).create();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
然后在这里使用上下文
final AlertDialog alertDialog=new AlertDialog.Builder(context).create();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
希望这会有所帮助。不起作用。使用警报类时方法中仍然存在相同的错误这是我使用private void free()的方式。{AlertClass AlertClass=new AlertClass();AlertClass.noInternetAlert(上下文);}这是一个错误,无法将android.app.Activity应用于android.content.contextfinal appcompativity Activity=(appcompativity)v.getContext();
您需要将其更改为final Context=v.getContext()
在适配器中。基本上确保将上下文而不是活动传递给AlertDialog.Builder
未工作。使用Alert类时方法中仍然存在相同错误这是我使用private void free()的方式{AlertClass AlertClass=new AlertClass();AlertClass.noInternetAlert(上下文);}这是一个错误,无法将android.app.Activity应用于android.content.contextfinal appcompativity Activity=(appcompativity)v.getContext();
您需要将其更改为final Context=v.getContext();
final AlertDialog alertDialog=new AlertDialog.Builder(context).create();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);