如何在android中单击listview中的按钮时获取自定义对话框活动?

Anw*_*mal 2 android android-adapter

这是我的ArrayAdapter代码.当我点击LinearLayout"cat"时,它会在dialog.show()上给出错误.我不知道如何在ArrayAdapter类中创建自定义对话框.当我删除创建对话框部分时,一切正常.

提前致谢

CategoryAdapter.java

public class CategoryAdapter extends ArrayAdapter<String> {
    private final Context context;

    String[] menu = new String[25] ;
    String[] menu2 = new String[25];
    String[] menu3 = new String[25];
    private LayoutInflater inflater;
    viewholder vh;
   
    public CategoryAdapter(Context context,    String [] menu,String [] menu2,String [] menu3) {
        super(context, R.layout.categoryadapter, menu);
        this.context = context;
        this.menu = menu;
        this.menu2=menu2;
        this.menu3=menu3;
    }
    public View getView(final int position, View convertView, ViewGroup parent) {
        {
            vh=new viewholder();
            if (inflater == null)
                inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.categoryadapter, parent, false);
            vh.cat=(LinearLayout) convertView.findViewById(R.id.category);
            convertView.setTag(vh);
        }

        vh.cat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Dialog dialog = new Dialog(getContext());
                dialog.setContentView(R.layout.update_categore_dialog);
                dialog.setTitle("Update Your Category");
                dialog.show();

                Toast.makeText(getContext(), "Clicked", Toast.LENGTH_LONG).show();
            }
        });

        return convertView;

    }
    public class viewholder
    {
        LinearLayout cat;
       
    }

}
Run Code Online (Sandbox Code Playgroud)

Int*_*iya 5

context改用getContext()

final Dialog dialog = new Dialog(context);
Run Code Online (Sandbox Code Playgroud)

最后,刚刚通过 context

vh.cat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.update_categore_dialog);
            dialog.setTitle("Update Your Category");
            dialog.show();

            Toast.makeText(getContext(), "Clicked", Toast.LENGTH_LONG).show();
        }
    });
Run Code Online (Sandbox Code Playgroud)