コード例 #1
0
  /**
   * Creates and shows a dialog for picking which of the active channels the user wants to show on
   * his android device. At least one is required
   */
  private void showDisplayChannels() {

    final ArrayList<String> channels = new ArrayList<String>(); // example {channel 1, channel 5}
    final ArrayList<String> sensors =
        new ArrayList<String>(); // example {blood pressure, temperature}

    // FILL THE CHANNELS AND SENSORS ARRAYS FOR THE LIST ADAPTER
    String[] activeSensors = newConfiguration.getActiveSensors();
    for (int i = 0; i < activeSensors.length; i++) {
      if (activeSensors[i].compareTo("null") != 0) {
        channels.add(getString(R.string.nc_dialog_channel) + " " + (i + 1));
        sensors.add(activeSensors[i]);
      }
    }

    final DisplayChannelsListAdapter displayChannelsListAdapter =
        new DisplayChannelsListAdapter(
            this, channels, sensors, newConfiguration.getDisplayChannels());
    AlertDialog displayChannelsDialog;

    TextView customTitleView = (TextView) inflater.inflate(R.layout.dialog_custom_title, null);
    customTitleView.setText(R.string.nc_dialog_title_channels_to_display);

    // BUILDER
    AlertDialog.Builder displayChannelsBuilder = new AlertDialog.Builder(this);
    displayChannelsBuilder
        .setCustomTitle(customTitleView)
        .setView(getLayoutInflater().inflate(R.layout.dialog_channels_listview, null))
        .setPositiveButton(
            getString(R.string.nc_dialog_positive_button),
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {

                channelsSelected = displayChannelsListAdapter.getChecked();
                String[] displayChannels = new String[8];

                if (numberOfChannelsSelected(channelsSelected) == 0) {
                  displayChannelsTV.setText("");
                  displayChannelsTV.setVisibility(View.GONE);
                } else {
                  displayChannelsTV.setVisibility(View.VISIBLE);
                  displayChannelsTV.setError(null);
                  displayChannelsTV.setTextColor(getResources().getColor(R.color.blue));
                  convertBooleanToString(displayChannels, channelsSelected);
                  newConfiguration.setDisplayChannels(displayChannels);
                  displayChannelsTV.setText(newConfiguration.getDisplayChannelsWithSensors());
                }
              }

              private void convertBooleanToString(
                  String[] channelsToDisplayArray, boolean[] channelsSelected) {
                for (int i = 0; i < channelsSelected.length; i++) {
                  if (channelsSelected[i]) {
                    int in =
                        Character.getNumericValue(
                            (channels
                                    .get(i)
                                    .toString()
                                    .charAt(channels.get(i).toString().length() - 1))
                                - 1);
                    channelsToDisplayArray[in] = sensors.get(i);
                  }
                }
              }
            });
    displayChannelsBuilder.setNegativeButton(
        getString(R.string.nc_dialog_negative_button),
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            // dismisses the dialog
          }
        });

    // CREATE DIALOG
    displayChannelsDialog = displayChannelsBuilder.create();
    displayChannelsDialog.show();

    // LIST VIEW CONFIGURATION
    ListView channelsToDisplayListView;
    channelsToDisplayListView =
        (ListView) displayChannelsDialog.findViewById(R.id.lv_channelsSelection);
    channelsToDisplayListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    channelsToDisplayListView.setItemsCanFocus(false);
    channelsToDisplayListView.setAdapter(displayChannelsListAdapter);
  }