【问题标题】:Not able to show Dialog box in Non Activity, RecyclerView Adapter Class无法在非活动、RecyclerView 适配器类中显示对话框
【发布时间】:2018-04-24 06:06:28
【问题描述】:

我在我的Fragment 中使用Recyclerview。我的RecyclerView 有Adapter 类。我需要以我在AdapterClass 中编写的一种方法显示AlertDialog。但我收到一个错误,因为

android.app.Activity 不能应用于 android.content.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();
        }
}  

这是我在AdapterClass 中的方法的代码

private void free() {
        AlertClass alert=new AlertClass();
        alert.noInternetAlert(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();
            }
        });

    }  

如何在这个方法中显示对话框?

【问题讨论】:

  • 尝试使用构造函数的上下文。无需使用最终的 AppCompatActivity activity=(AppCompatActivity)v.getContext(); .你已经有了上下文。

标签: android android-fragments android-recyclerview android-context android-dialog


【解决方案1】:

只需使用 v.getContext 像这样更改AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getContext);

  holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
         AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getContext);

        // Setting Dialog Title
        alertDialog.setTitle("Confirm Delete...");

        // Setting Dialog Message
        alertDialog.setMessage("Are you sure you want delete this?");

        // Setting Icon to Dialog
        alertDialog.setIcon(R.drawable.delete);

        // Setting Positive "Yes" Button
        alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            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();
            }
        });

        // Setting Negative "NO" Button
        alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            // Write your code here to invoke NO event

            dialog.cancel();
            }
        });

        // Showing Alert Message
        alertDialog.show();
      }
});

【讨论】:

    【解决方案2】:

    你需要在AlertDialog.Builder中使用Context而不是Activity

    public void noInternetAlert(Activity activity)
    {
        Context context = activity;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        ....
    }
    

    【讨论】:

    • 不工作。在使用警报类时方法中仍然存在同样的错误
    • 这是我使用 private void free() { AlertClass alertClass=new AlertClass(); alertClass.noInternetAlert(context);} 这是错误 android.app.Activity 不能应用于 android.content.context
    • final AppCompatActivity activity = (AppCompatActivity)v.getContext(); 您需要在适配器中将其更改为 final Context context = v.getContext();。基本上确保您将上下文而不是活动传递给AlertDialog.Builder
    【解决方案3】:

    适配器类中已经有了上下文。您需要使用上下文来创建 alertDialog。

    先去掉这一行

    final AppCompatActivity activity=(AppCompatActivity)v.getContext();
    

    然后在这里使用上下文。

    final AlertDialog alertDialog=new AlertDialog.Builder(context).create();
    final AlertDialog.Builder builder = new AlertDialog.Builder(context);
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-22
      • 2021-05-09
      • 1970-01-01
      相关资源
      最近更新 更多