/**
   * Gets a View that displays the given group. This View is only for the group--the Views for the
   * group's children will be fetched using {@link #getChildView(int, int, boolean, View,
   * ViewGroup)}.
   *
   * @param groupPosition the position of the group for which the View is returned
   * @param isExpanded whether the group is expanded or collapsed
   * @param convertView the old view to reuse, if possible. You should check that this view is
   *     non-null and of an appropriate type before using. If it is not possible to convert this
   *     view to display the correct data, this method can create a new view. It is not guaranteed
   *     that the convertView will have been previously created by {@link #getGroupView(int,
   *     boolean, View, ViewGroup)}.
   * @param parent the parent that this view will eventually be attached to
   * @return the View corresponding to the group at the specified position
   */
  @SuppressWarnings("JavaDoc")
  @SuppressLint("InflateParams")
  @Override
  public View getGroupView(
      final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    String courseName = ((Course) getGroup(groupPosition)).getCourseTitle();
    if (convertView == null) {
      LayoutInflater inflater =
          (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      convertView = inflater.inflate(R.layout.list_group_course, null);
    }

    TextView courseNameTextView =
        (TextView) convertView.findViewById(R.id.courseExpandableListViewTitle);
    CheckBox courseCheckbox =
        (CheckBox) convertView.findViewById(R.id.courseExpandableListViewCheckbox);
    Button courseDelete =
        (Button) convertView.findViewById(R.id.courseExpandableListViewDeleteButton);

    final Context tempContext = context;

    courseDelete.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            try {
              confirmDelete(groupPosition, tempContext);
            } catch (WindowManager.BadTokenException e) {
              courseArrayList.remove(getGroup(groupPosition));
              SelectBlockoutTimes.saveBlockoutCoursesToFile(context, courseArrayList);
              notifyDataSetChanged();
            }
          }
        });

    CheckListener checkListener = new CheckListener(groupPosition);
    courseCheckbox.setFocusable(false);
    courseCheckbox.setOnCheckedChangeListener(checkListener);

    courseNameTextView.setTextColor(Color.BLACK);
    courseNameTextView.setTypeface(null, Typeface.BOLD);
    courseNameTextView.setText(courseName);

    return convertView;
  }
    @SuppressWarnings("unchecked")
    public ListMultiWidget(Context context, FormEntryPrompt prompt, boolean displayLabel) {
        super(context, prompt);

        mItems = prompt.getSelectChoices();
        mCheckboxes = new ArrayList<CheckBox>();
        mPrompt = prompt;

        // Layout holds the horizontal list of buttons
        LinearLayout buttonLayout = new LinearLayout(context);

        Vector<Selection> ve = new Vector<Selection>();
        if (prompt.getAnswerValue() != null) {
            ve = (Vector<Selection>) prompt.getAnswerValue().getValue();
        }

        if (mItems != null) {
            for (int i = 0; i < mItems.size(); i++) {
                CheckBox c = new CheckBox(getContext());
                c.setTag(Integer.valueOf(i));
                c.setId(QuestionWidget.newUniqueId());
                c.setFocusable(!prompt.isReadOnly());
                c.setEnabled(!prompt.isReadOnly());
                for (int vi = 0; vi < ve.size(); vi++) {
                    // match based on value, not key
                    if (mItems.get(i).getValue().equals(ve.elementAt(vi).getValue())) {
                        c.setChecked(true);
                        break;
                    }

                }
                mCheckboxes.add(c);

                // when clicked, check for readonly before toggling
                c.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (!mCheckboxInit && mPrompt.isReadOnly()) {
                            if (buttonView.isChecked()) {
                                buttonView.setChecked(false);
                               	Collect.getInstance().getActivityLogger().logInstanceAction(this, "onItemClick.deselect",
                            			mItems.get((Integer)buttonView.getTag()).getValue(), mPrompt.getIndex());
                            } else {
                                buttonView.setChecked(true);
                               	Collect.getInstance().getActivityLogger().logInstanceAction(this, "onItemClick.select",
                            			mItems.get((Integer)buttonView.getTag()).getValue(), mPrompt.getIndex());
                            }
                        }
                    }
                });

                String imageURI = null;
                imageURI =
                    prompt.getSpecialFormSelectChoiceText(mItems.get(i),
                        FormEntryCaption.TEXT_FORM_IMAGE);

                // build image view (if an image is provided)
                ImageView mImageView = null;
                TextView mMissingImage = null;

                final int labelId = QuestionWidget.newUniqueId();

                // Now set up the image view
                String errorMsg = null;
                if (imageURI != null) {
                    try {
                        String imageFilename =
                            ReferenceManager._().DeriveReference(imageURI).getLocalURI();
                        final File imageFile = new File(imageFilename);
                        if (imageFile.exists()) {
                            Bitmap b = null;
                            try {
                                Display display =
                                    ((WindowManager) getContext().getSystemService(
                                        Context.WINDOW_SERVICE)).getDefaultDisplay();
                                int screenWidth = display.getWidth();
                                int screenHeight = display.getHeight();
                                b =
                                    FileUtils.getBitmapScaledToDisplay(imageFile, screenHeight,
                                        screenWidth);
                            } catch (OutOfMemoryError e) {
                                errorMsg = "ERROR: " + e.getMessage();
                            }

                            if (b != null) {
                                mImageView = new ImageView(getContext());
                                mImageView.setPadding(2, 2, 2, 2);
                                mImageView.setAdjustViewBounds(true);
                                mImageView.setImageBitmap(b);
                                mImageView.setId(labelId);
                            } else if (errorMsg == null) {
                                // An error hasn't been logged and loading the image failed, so it's
                                // likely
                                // a bad file.
                                errorMsg = getContext().getString(R.string.file_invalid, imageFile);

                            }
                        } else if (errorMsg == null) {
                            // An error hasn't been logged. We should have an image, but the file
                            // doesn't
                            // exist.
                            errorMsg = getContext().getString(R.string.file_missing, imageFile);
                        }

                        if (errorMsg != null) {
                            // errorMsg is only set when an error has occured
                            Log.e(t, errorMsg);
                            mMissingImage = new TextView(getContext());
                            mMissingImage.setText(errorMsg);

                            mMissingImage.setPadding(2, 2, 2, 2);
                            mMissingImage.setId(labelId);
                        }
                    } catch (InvalidReferenceException e) {
                        Log.e(t, "image invalid reference exception");
                        e.printStackTrace();
                    }
                } else {
                    // There's no imageURI listed, so just ignore it.
                }

                // build text label. Don't assign the text to the built in label to he
                // button because it aligns horizontally, and we want the label on top
                TextView label = new TextView(getContext());
                label.setText(prompt.getSelectChoiceText(mItems.get(i)));
                label.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mAnswerFontsize);
                label.setGravity(Gravity.CENTER_HORIZONTAL);
                if (!displayLabel) {
                    label.setVisibility(View.GONE);
                }

                // answer layout holds the label text/image on top and the radio button on bottom
                RelativeLayout answer = new RelativeLayout(getContext());
                RelativeLayout.LayoutParams headerParams =
                        new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                headerParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                headerParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
                
                RelativeLayout.LayoutParams buttonParams =
                        new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

                if (mImageView != null) {
                	mImageView.setScaleType(ScaleType.CENTER);
                    if (!displayLabel) {
                        mImageView.setVisibility(View.GONE);
                    }
                    answer.addView(mImageView, headerParams);
                } else if (mMissingImage != null) {
                    answer.addView(mMissingImage, headerParams);
                } else {
                    if (displayLabel) {
                    	label.setId(labelId);
                        answer.addView(label, headerParams);
                    }

                }
                if (displayLabel) {
                	buttonParams.addRule(RelativeLayout.BELOW, labelId);
                }
                answer.addView(c, buttonParams);
                answer.setPadding(4, 0, 4, 0);

                // /Each button gets equal weight
                LinearLayout.LayoutParams answerParams =
                    new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT);
                answerParams.weight = 1;

                buttonLayout.addView(answer, answerParams);

            }
        }

        // Align the buttons so that they appear horizonally and are right justified
        // buttonLayout.setGravity(Gravity.RIGHT);
        buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
        // LinearLayout.LayoutParams params = new
        // LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        // buttonLayout.setLayoutParams(params);

        // The buttons take up the right half of the screen
        LinearLayout.LayoutParams buttonParams =
            new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        buttonParams.weight = 1;

        questionLayout.addView(buttonLayout, buttonParams);
        addView(questionLayout);

    }
  @SuppressWarnings("unchecked")
  public SelectMultiWidget(Context context, FormEntryPrompt prompt) {
    super(context, prompt);
    mPrompt = prompt;
    mCheckboxes = new ArrayList<CheckBox>();

    // SurveyCTO-added support for dynamic select content (from .csv files)
    XPathFuncExpr xPathFuncExpr =
        ExternalDataUtil.getSearchXPathExpression(prompt.getAppearanceHint());
    if (xPathFuncExpr != null) {
      mItems = ExternalDataUtil.populateExternalChoices(prompt, xPathFuncExpr);
    } else {
      mItems = prompt.getSelectChoices();
    }

    setOrientation(LinearLayout.VERTICAL);

    Vector<Selection> ve = new Vector<Selection>();
    if (prompt.getAnswerValue() != null) {
      ve = (Vector<Selection>) prompt.getAnswerValue().getValue();
    }

    if (mItems != null) {
      for (int i = 0; i < mItems.size(); i++) {
        // no checkbox group so id by answer + offset
        CheckBox c = new CheckBox(getContext());
        c.setTag(Integer.valueOf(i));
        c.setId(QuestionWidget.newUniqueId());
        c.setText(prompt.getSelectChoiceText(mItems.get(i)));
        c.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mAnswerFontsize);
        c.setFocusable(!prompt.isReadOnly());
        c.setEnabled(!prompt.isReadOnly());

        for (int vi = 0; vi < ve.size(); vi++) {
          // match based on value, not key
          if (mItems.get(i).getValue().equals(ve.elementAt(vi).getValue())) {
            c.setChecked(true);
            break;
          }
        }
        mCheckboxes.add(c);
        // when clicked, check for readonly before toggling
        c.setOnCheckedChangeListener(
            new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!mCheckboxInit && mPrompt.isReadOnly()) {
                  if (buttonView.isChecked()) {
                    buttonView.setChecked(false);
                    Collect.getInstance()
                        .getActivityLogger()
                        .logInstanceAction(
                            this,
                            "onItemClick.deselect",
                            mItems.get((Integer) buttonView.getTag()).getValue(),
                            mPrompt.getIndex());
                  } else {
                    buttonView.setChecked(true);
                    Collect.getInstance()
                        .getActivityLogger()
                        .logInstanceAction(
                            this,
                            "onItemClick.select",
                            mItems.get((Integer) buttonView.getTag()).getValue(),
                            mPrompt.getIndex());
                  }
                }
              }
            });

        String audioURI = null;
        audioURI =
            prompt.getSpecialFormSelectChoiceText(mItems.get(i), FormEntryCaption.TEXT_FORM_AUDIO);

        String imageURI;
        if (mItems.get(i) instanceof ExternalSelectChoice) {
          imageURI = ((ExternalSelectChoice) mItems.get(i)).getImage();
        } else {
          imageURI =
              prompt.getSpecialFormSelectChoiceText(
                  mItems.get(i), FormEntryCaption.TEXT_FORM_IMAGE);
        }

        String videoURI = null;
        videoURI = prompt.getSpecialFormSelectChoiceText(mItems.get(i), "video");

        String bigImageURI = null;
        bigImageURI = prompt.getSpecialFormSelectChoiceText(mItems.get(i), "big-image");

        MediaLayout mediaLayout = new MediaLayout(getContext());
        mediaLayout.setAVT(
            prompt.getIndex(),
            "." + Integer.toString(i),
            c,
            audioURI,
            imageURI,
            videoURI,
            bigImageURI);
        addView(mediaLayout);

        // Last, add the dividing line between elements (except for the last element)
        ImageView divider = new ImageView(getContext());
        divider.setBackgroundResource(android.R.drawable.divider_horizontal_bright);
        if (i != mItems.size() - 1) {
          addView(divider);
        }
      }
    }

    mCheckboxInit = false;
  }
Exemple #4
0
  @Override
  public View getView(final Context context) {
    LinearLayout l = new LinearLayout(context);
    l.setGravity(Gravity.CENTER_VERTICAL);
    l.setPadding(DEFAULT_PADDING, DEFAULT_PADDING, DEFAULT_PADDING, DEFAULT_PADDING);

    TextView t = new TextView(context);
    t.setLayoutParams(
        new LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, weightOfDescription));
    t.setText(this.getVarName());
    l.addView(t);

    t.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            if (checkbox != null && checkbox.isEnabled()) {
              checkbox.setChecked(!checkbox.isChecked());
            }
          }
        });

    checkbox = (CheckBox) View.inflate(context, R.layout.material_factory_checkbox, null);

    checkbox.setChecked(loadVar());
    checkbox.setEnabled(editable);
    checkbox.setFocusable(editable);
    checkbox.setOnCheckedChangeListener(
        new OnCheckedChangeListener() {

          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (!M_Checkbox.this.onCheckedChanged(context, checkbox, isChecked)) {
              checkbox.setChecked(!isChecked);
            }
          }
        });

    LinearLayout l2 = new LinearLayout(context);
    l2.setLayoutParams(
        new LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, weightOfInputText));
    l2.setGravity(Gravity.RIGHT);
    l2.addView(checkbox);
    l.addView(l2);

    if (myDecorator != null) {
      int level = myDecorator.getCurrentLevel();
      myDecorator.decorate(context, t, level, UiDecorator.TYPE_INFO_TEXT);
      myDecorator.decorate(context, checkbox, level, UiDecorator.TYPE_EDIT_TEXT);
    }

    if (checkboxCreatedListener != null) {
      checkboxCreatedListener.onUiCreated(checkbox);
    }
    if (textCreatedListener != null) {
      textCreatedListener.onUiCreated(t);
    }

    return l;
  }
  @SuppressWarnings("unchecked")
  public void buildView(final PromptElement prompt) {
    mItems = prompt.getSelectItems();

    setOrientation(LinearLayout.VERTICAL);

    Vector ve = new Vector();
    if (prompt.getAnswerValue() != null) {
      ve = (Vector) prompt.getAnswerObject();
    }

    if (prompt.getSelectItems() != null) {
      OrderedHashtable h = prompt.getSelectItems();
      Enumeration en = h.keys();
      String k = null;
      String v = null;

      // counter for offset
      int i = 0;

      while (en.hasMoreElements()) {

        k = (String) en.nextElement();
        v = (String) h.get(k);

        // no checkbox group so id by answer + offset
        CheckBox c = new CheckBox(getContext());

        // when clicked, check for readonly before toggling
        c.setOnCheckedChangeListener(
            new CompoundButton.OnCheckedChangeListener() {
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!mCheckboxInit && prompt.isReadonly()) {
                  if (buttonView.isChecked()) {
                    buttonView.setChecked(false);
                  } else {
                    buttonView.setChecked(true);
                  }
                }
              }
            });

        c.setId(CHECKBOX_ID + i);
        c.setText(k);
        c.setTextSize(TypedValue.COMPLEX_UNIT_PT, GlobalConstants.APPLICATION_FONTSIZE);

        for (int vi = 0; vi < ve.size(); vi++) {
          // match based on value, not key
          if (v.equals(((Selection) ve.elementAt(vi)).getValue())) {
            c.setChecked(true);
            break;
          }
        }

        c.setFocusable(!prompt.isReadonly());
        c.setEnabled(!prompt.isReadonly());
        addView(c);
        i++;
      }
    }

    mCheckboxInit = false;
  }
  @SuppressWarnings("unchecked")
  public SelectMultiWidget(Context context, FormEntryPrompt prompt) {
    super(context, prompt);
    mPrompt = prompt;
    mCheckboxes = new Vector<CheckBox>();
    mItems = prompt.getSelectChoices();

    setOrientation(LinearLayout.VERTICAL);

    Vector<Selection> ve = new Vector<Selection>();
    if (prompt.getAnswerValue() != null) {
      ve = (Vector<Selection>) getCurrentAnswer().getValue();
    }

    // Is this safe enough from collisions?
    buttonIdBase = Math.abs(prompt.getIndex().toString().hashCode());

    if (prompt.getSelectChoices() != null) {
      for (int i = 0; i < mItems.size(); i++) {
        // no checkbox group so id by answer + offset
        final CheckBox c = new CheckBox(getContext());

        c.setId(buttonIdBase + i);
        c.setText(stylize(prompt.getSelectChoiceText(mItems.get(i))));
        c.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mAnswerFontsize);
        c.setFocusable(!prompt.isReadOnly());
        c.setEnabled(!prompt.isReadOnly());

        int padding = (int) Math.floor(context.getResources().getDimension(R.dimen.select_padding));

        c.setPadding(c.getPaddingLeft(), 0, padding, 0);
        for (int vi = 0; vi < ve.size(); vi++) {
          // match based on value, not key
          if (mItems.get(i).getValue().equals(ve.elementAt(vi).getValue())) {
            c.setChecked(true);
            break;
          }
        }

        // Note: This gets fired during setup as well, so this listener should only
        // be added after everything about the checkbox is set up

        // when clicked, check for readonly before toggling
        c.setOnCheckedChangeListener(
            new CompoundButton.OnCheckedChangeListener() {
              /*
               * (non-Javadoc)
               * @see android.widget.CompoundButton.OnCheckedChangeListener#onCheckedChanged(android.widget.CompoundButton, boolean)
               */
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!mCheckboxInit && mPrompt.isReadOnly()) {
                  if (buttonView.isChecked()) {
                    buttonView.setChecked(false);
                  } else {
                    buttonView.setChecked(true);
                  }
                }
                widgetEntryChanged();
              }
            });

        mCheckboxes.add(c);

        String audioURI = null;
        audioURI =
            prompt.getSpecialFormSelectChoiceText(mItems.get(i), FormEntryCaption.TEXT_FORM_AUDIO);

        String imageURI = null;
        imageURI =
            prompt.getSpecialFormSelectChoiceText(mItems.get(i), FormEntryCaption.TEXT_FORM_IMAGE);

        String videoURI = null;
        videoURI = prompt.getSpecialFormSelectChoiceText(mItems.get(i), "video");

        String bigImageURI = null;
        bigImageURI = prompt.getSpecialFormSelectChoiceText(mItems.get(i), "big-image");

        MediaLayout mediaLayout = new MediaLayout(getContext());
        mediaLayout.setAVT(c, audioURI, imageURI, videoURI, bigImageURI);
        addView(mediaLayout);

        mediaLayout.setPadding(0, padding, 0, padding);

        mediaLayout.setOnClickListener(
            new OnClickListener() {

              @Override
              public void onClick(View v) {
                c.performClick();
              }
            });

        // Last, add the dividing line between elements (except for the last element)
        ImageView divider = new ImageView(getContext());
        divider.setBackgroundResource(android.R.drawable.divider_horizontal_bright);
        if (i != mItems.size() - 1) {
          addView(divider);
        }
      }
    }

    mCheckboxInit = false;
  }