如何在androidstudio中从一个又一个对话框的oncreateoptions菜单中获取变量

sg24os4d  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(198)

OnCreateOptions菜单

public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main , menu);
        MenuItem itemSwitch = menu.findItem(R.id.myswitch);
        itemSwitch.setActionView(R.layout.switch_layout);
        final Switch sv = (Switch) menu.findItem(R.id.myswitch).getActionView().findViewById(R.id.switchForActionBar);
        sv.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(getApplicationContext(),"Led Opened.",Toast.LENGTH_LONG).show();
                }
                else{
                    Toast.makeText(getApplicationContext(),"Led Closed.",Toast.LENGTH_LONG).show();

                }
            }
        });

        MenuItem itemTv = menu.findItem(R.id.mytextview);
        itemTv.setActionView(R.layout.clickable_text_view);
        final TextView tvDevicesAndGroups = (TextView)menu.findItem(R.id.mytextview).getActionView().findViewById(R.id.tvforactionBar);
        tvDevicesAndGroups.setText("Devices/Groups");
        tvDevicesAndGroups.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View v) {
                    String query_room = "SELECT DISTINCT Room FROM Devices";
                    String res = "";
                    dialog(query_room);
                    tvDevicesAndGroups.setText(selected_Devices);

                }
        });
        return true;
    }

Get Arraylist from sqlite db

    ArrayList<String> getGroups(String query){
        ArrayList<String> groups = new ArrayList<>();
        Cursor cursorRoom = dbh.getSpecificQuery(query);
        String title;
        int item=0;

            if(cursorRoom.moveToFirst()) {
                do {
                    title = cursorRoom.getString(0);
                    if(title!=null)
                    {
                        groups.add(title);

                    }
                        // Cursor cursor_dev = dbh.getSpecificQuery("Select Name , Addr From Devices WHERE Room='"+title+"'");
                } while (cursorRoom.moveToNext());
            }

        return groups;

    }

对话框-1

void dialog(String query) {
        ArrayAdapter<String> group_names;
        group_names = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getGroups(query));
        final String[] title = new String[1];
   //     Toast.makeText(getApplicationContext(),group_names.getItem(0),Toast.LENGTH_SHORT).show();
        AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
        //alt_bld.setIcon(R.drawable.icon);
        alt_bld.setTitle("Select a Group Name");
        alt_bld.setSingleChoiceItems((ListAdapter) group_names, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(getApplicationContext(),
                        "Group Name = " + group_names.getItem(item), Toast.LENGTH_SHORT).show();
                title[0] = group_names.getItem(item);

            }
        });
        alt_bld.setPositiveButton("SELECT DEVICE", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

在这里获取另一个对话框并将所选项目作为title show_dialog发送(“select name,addr from devices where room='”+标题[0]+“'”,标题[0]);dialog.dismise();}});

alt_bld.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                return;

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

Dialog - 2

        public void show_dialog(String query, String title) {

        ArrayAdapter<String> group_names;
        ArrayAdapter<String> results;
        final String[] dev = new String[1];
        group_names = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getGroups(query));

        final String[] selected_item = new String[1];
        //     Toast.makeText(getApplicationContext(),group_names.getItem(0),Toast.LENGTH_SHORT).show();
        AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
        //alt_bld.setIcon(R.drawable.icon);
        alt_bld.setTitle("Select a Group Name");
        alt_bld.setSingleChoiceItems((ListAdapter) group_names, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(getApplicationContext(),
                        "Group Name = " + group_names.getItem(item), Toast.LENGTH_SHORT).show();
                dev[0] = group_names.getItem(item);
                //results.add(group_names.getItem(item));

            }
        });
        alt_bld.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

所选设备是一个全局变量,在此处更改,但我无法编写actionbar textview。所选设备=标题+“/”+开发[0];dialog.dismise();

}
        });
        alt_bld.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                return;

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

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题