如何在 android 中单击列表视图中的按钮时获得自定义对话框 activity?

How to get custom dialog activity while clicking on a button in listview in android?

here is my code for ArrayAdapter. When i click on LinearLayout "cat" it gives error on dialog.show(). I don't know how to create custom dialog within ArrayAdapter class. Everything work fine when i remove creating dialog part.

Thanks in advance

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;
       
    }

}

使用context代替getContext()

final Dialog dialog = new Dialog(context);

最后,就通过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();
        }
    });

尝试-

 Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.update_categore_dialog);
                dialog.setTitle("Update Your Category");
                dialog.show();

试试下面这个 link,希望你喜欢。

OrderDetailListAdatper adapter = new OrderDetailListAdatper(Yourclass.this,Resource,
    listorderlistInfo);
//set your adapter..

在您的适配器 getview 中,粘贴该代码,它对我来说工作正常。

holder.btnDelete.setOnClickListener(new OnClickListener() { 
@Override
public void onClick(View view) {                

    AlertDialog.Builder alert = new AlertDialog.Builder((Activity)_context);

    alert.setMessage("Do you want to delete?");
        alert.setNegativeButton("CANCEL",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(
                        DialogInterface dialog,
                        int whichButton) {
                    dialog.cancel();
                }
            });
    alert.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(
                        DialogInterface dialog,
                        int whichButton) {

                    OrderDetailListAdatper.this._listOrderListInfoAdapter
                            .remove(position);                          OrderDetailListAdatper.thisnotifyDataSetChanged();                                  
                }
            });
    alert.create().show(); // btw show() creates and shows it..
}
});

在您的 getView() 方法中,将 getContext() 替换为您的 context 变量。

vh.cat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Dialog dialog = new Dialog(context);
                //remaining code...
            }
        });

并且在 CateogoryAdapter 的初始化中不要传递 getApplicationContext(),而是像这样传递上下文 (Activity.this)。

CategoryAdapter adapter = CategoryAdapter(YourActivity.this, menu, menu2, menu3)