通过警报对话框上的单选按钮继续下一个活动

时间:2015-09-16 06:57:37

标签: android android-intent radio-button alertdialog

我有listview,点击每个列表项,它必须弹出带有单选按钮的警报。选择一个单选按钮选项,然后单击"确定"警告对话框上的按钮,我必须能够继续下一个活动。 (PS我不想使用正面,负面按钮)。

下面是我的代码,listview工作正常,警告对话框弹出,选择是或否,Toast show。但是,是的,它不会进行到下一个活动。请帮忙!!

listview = (ListView) findViewById(R.id.mylistview);
    final String[] items = new String[]{"IOS", "ANDROID", "WINDOWS"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_expandable_list_item_1, items);
    listview.setAdapter(adapter);

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int itemposition = position;
            String itemvalue = (String) listview.getItemAtPosition(position);
            final CharSequence[] items1 = {"yes", "no"};
            final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("slection confirmation");
            builder.setSingleChoiceItems(items1, -1, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getApplicationContext(), items1[which], Toast.LENGTH_SHORT).show();
                }


            });
            builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch(items1.toString())
                    {
                        case("yes"):
                            Intent myint=new Intent(MainActivity.this,secondpage.class);
                            myint.putExtra("act1","");
                            startActivity(myint);
                            break;
                        case("no"):
                            dialog.cancel();

                    }
               }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
    });

}

}

1 个答案:

答案 0 :(得分:0)

您在下面的代码段中遇到问题,当您将整个字符串数组转换为字符串时,您需要及时获取一个项目。

 switch(items1.toString())
                    {
                        case("yes"):
                            Intent myint=new Intent(MainActivity.this,secondpage.class);
                            myint.putExtra("act1","");
                            startActivity(myint);
                            break;
                        case("no"):
                            dialog.cancel();

                    }

请用

替换它
String selection;
     listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                int itemposition = position;
                String itemvalue = (String) listview.getItemAtPosition(position);
                final CharSequence[] items1 = {"yes", "no"};
                final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("slection confirmation");
                builder.setSingleChoiceItems(items1, -1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

    selection = items1[which]
                        Toast.makeText(getApplicationContext(), items1[which], Toast.LENGTH_SHORT).show();
                    }


                });
                builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch(selection)
                        {
                            case("yes"):
                                Intent myint=new Intent(MainActivity.this,secondpage.class);
                                myint.putExtra("act1","");
                                startActivity(myint);
                                break;
                            case("no"):
                                dialog.cancel();

                        }
                   }
                });
                AlertDialog alert = builder.create();
                alert.show();
            }
        });