【问题标题】:How To set a Dialog Box on clicking a Gridview item?如何在单击 Gridview 项目时设置对话框?
【发布时间】:2017-09-27 22:32:44
【问题描述】:

我是安卓新手。当我单击gridview 项目时,我希望有一个带有正负按钮的对话框。现在,当我单击一个项目时,它会使用我单击的项目的信息填充另一个活动。

例如,使用 putExtragetExtras 填充活动效果很好。现在我想要的是,我希望在Alertdialog 框的ok 按钮上执行操作,而不是在gridView 上执行操作。

我应该如何设置gridview.setOnCLickListener,以便在单击项目时弹出一个alertDialog 框??

         gridView.setOnItemClickListener(new 
         AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int 
             position, long id) {
            gridView.setClickable(true);
        item= (Hotel) parent.getItemAtPosition(position);

                                    String names = item.getName();
                                    Intent i=new 
             Intent(HotelList.this,UserViewActivity.class);
                                    i.putExtra("names",names);
                                    startActivity(i);

        }

    });

【问题讨论】:

标签: java android


【解决方案1】:

首先,移除 gridView.setClickable(true)。这根本没有必要。其次,您可以在代码中创建对话框:

AlertDialog ad = new AlertDialog.Builder(this).setContentView(R.layout.activity_dialog).show();

【讨论】:

    【解决方案2】:

    试试下面的代码:

     @Override
        public void onItemClick(AdapterView<?> parent, View view, int 
             position, long id) {
    
            item= (Hotel) parent.getItemAtPosition(position);
    
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setCancelable(false);
            dialog.setTitle("Your Title");
            dialog.setMessage("Your Message" );
            dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                String names = item.getName();
                Intent i= new Intent(HotelList.this,UserViewActivity.class);
                i.putExtra("names",names);
                startActivity(i);
                alert.cancel();
              }
            })
            .setNegativeButton("Cancel ", new DialogInterface.OnClickListener() 
            {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                alert.cancel();
                }
            });
    
            final AlertDialog alert = dialog.create();
            alert.show();
        }
    
    });
    

    希望这会有所帮助。

    【讨论】:

    • 我的荣幸。如果有效,您可以通过单击左侧的勾号将答案标记为已接受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 2011-11-23
    • 1970-01-01
    • 2020-07-20
    • 2014-06-28
    相关资源
    最近更新 更多