@Override
  public void onClick(View view) {
    if (view.getId() == R.id.detail_send) {
      mProgressDialog.show();
      responseString = mEditText.getText().toString();
      sendMessageToServer();
    } else if (view.getId() == R.id.option_reply) {
      final RadioGroup radioGroup = new RadioGroup(MessageDetailActivity.this);
      radioGroup.setBackgroundColor(getResources().getColor(R.color.default_background));
      for (int i = 0; i < options.length; i++) {
        RadioButton radioButton = new RadioButton(this);
        radioButton.setText(options[i]);
        if (i == 0) {
          radioButton.setChecked(true);
          responseString = options[0];
        }
        radioButton.setId(i);
        radioGroup.addView(radioButton);
      }
      radioGroup.setId(-1);
      radioGroup.setOnCheckedChangeListener(
          new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
              responseString = options[checkedId];
            }
          });

      new AlertDialog.Builder(this)
          .setTitle(getString(R.string.reply))
          .setView(radioGroup)
          .setCancelable(false)
          .setPositiveButton(
              getString(R.string.ok),
              new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                  mProgressDialog.show();
                  sendMessageToServer();
                }
              })
          .show();
    }
  }
  private void createRadio() {
    // データ取得
    Cursor cursor =
        productDB.query("product_info", COLUMNS, null, null, null, null, "product_category");

    // LinearLayoutオブジェクト取得
    if (lLayout == null) {
      lLayout = (LinearLayout) this.findViewById(R.id.itemInfo);
    }
    lLayout.removeAllViews(); // LinearLayout初期化

    if (cursor.moveToFirst()) {
      // RadioGroup生成(レコードが1件以上の場合)
      RadioGroup rGroup = new RadioGroup(this);
      rGroup.setId(0);

      do {
        // 品物情報を保持
        this.keepItemInfo(cursor);

        // RadioButton生成
        RadioButton rButton = new RadioButton(this);
        rButton.setId(cursor.getInt(0));
        String itemName = " 品物名: " + cursor.getString(1) + "\n 種別:  " + cursor.getString(2);
        rButton.setText(itemName);
        rGroup.addView(rButton); // RadioGroupにRadioButtonを追加
      } while (cursor.moveToNext());
      lLayout.addView(rGroup); // LinearLayoutにRadioGroupを追加
    } else {
      // TextView生成(レコードが0件のとき)
      TextView nothing = new TextView(this);
      nothing.setText("品物は現在登録されていません。");
      lLayout.addView(nothing); // LinearLayoutにTextViewを追加
    }
    // 検索結果クリア
    cursor.close();
  }
  @Override
  public void drawQuestionUI(LayoutInflater inflater, final RelativeLayout relativeLayout) {
    super.drawQuestionUI(inflater, relativeLayout);
    radioGroup = (RadioGroup) inflater.inflate(R.layout.radiogroup, null);
    Integer lastId = getLastId();
    for (String answer : getAnswerList()) {
      RadioButton radioButton = (RadioButton) inflater.inflate(R.layout.radiobutton, null);
      radioButton.setText(answer);
      radioButton.setId(getId());
      increaseId();
      radioGroup.addView(radioButton);
      radioButton.setOnClickListener(
          new OnClickListener() {

            @Override
            public void onClick(View v) {
              updateSubmitButton(relativeLayout);
            }
          });
    }
    radioGroup.setId(getId());
    relativeLayout.addView(radioGroup, getLayoutParameter(lastId));
    increaseId();
  }
Beispiel #4
0
  protected ItemsetWidget(
      Context context, FormEntryPrompt prompt, boolean readOnlyOverride, boolean derived) {
    super(context, prompt);
    mButtons = new RadioGroup(context);
    mButtons.setId(QuestionWidget.newUniqueId());
    mReadOnly = prompt.isReadOnly() || readOnlyOverride;
    mAnswers = new HashMap<String, String>();

    String currentAnswer = prompt.getAnswerText();

    // the format of the query should be something like this:
    // query="instance('cities')/root/item[state=/data/state and county=/data/county]"

    // "query" is what we're using to notify that this is an
    // itemset widget.
    String nodesetStr = prompt.getQuestion().getAdditionalAttribute(null, "query");

    // parse out the list name, between the ''
    String list_name =
        nodesetStr.substring(nodesetStr.indexOf("'") + 1, nodesetStr.lastIndexOf("'"));

    // isolate the string between between the [ ] characters
    String queryString =
        nodesetStr.substring(nodesetStr.indexOf("[") + 1, nodesetStr.lastIndexOf("]"));

    StringBuilder selection = new StringBuilder();
    // add the list name as the first argument, which will always be there
    selection.append("list_name=?");

    // check to see if there are any arguments
    if (queryString.indexOf("=") != -1) {
      selection.append(" and ");
    }

    // can't just split on 'and' or 'or' because they have different
    // behavior, so loop through and break them off until we don't have any
    // more
    // must include the spaces in indexOf so we don't match words like
    // "land"
    int andIndex = -1;
    int orIndex = -1;
    ArrayList<String> arguments = new ArrayList<String>();
    while ((andIndex = queryString.indexOf(" and ")) != -1
        || (orIndex = queryString.indexOf(" or ")) != -1) {
      if (andIndex != -1) {
        String subString = queryString.substring(0, andIndex);
        String pair[] = subString.split("=");
        if (pair.length == 2) {
          selection.append(pair[0].trim() + "=? and ");
          arguments.add(pair[1].trim());
        } else {
          // parse error
        }
        // move string forward to after " and "
        queryString = queryString.substring(andIndex + 5, queryString.length());
        andIndex = -1;
      } else if (orIndex != -1) {
        String subString = queryString.substring(0, orIndex);
        String pair[] = subString.split("=");
        if (pair.length == 2) {
          selection.append(pair[0].trim() + "=? or ");
          arguments.add(pair[1].trim());
        } else {
          // parse error
        }

        // move string forward to after " or "
        queryString = queryString.substring(orIndex + 4, queryString.length());
        orIndex = -1;
      }
    }

    // parse the last segment (or only segment if there are no 'and' or 'or'
    // clauses
    String pair[] = queryString.split("=");
    if (pair.length == 2) {
      selection.append(pair[0].trim() + "=?");
      arguments.add(pair[1].trim());
    }
    if (pair.length == 1) {
      // this is probably okay, because then you just list all items in
      // the list
    } else {
      // parse error
    }

    // +1 is for the list_name
    String[] selectionArgs = new String[arguments.size() + 1];

    boolean nullArgs = false; // can't have any null arguments
    selectionArgs[0] = list_name; // first argument is always listname

    // loop through the arguments, evaluate any expressions
    // and build the query string for the DB
    for (int i = 0; i < arguments.size(); i++) {
      XPathExpression xpr = null;
      try {
        xpr = XPathParseTool.parseXPath(arguments.get(i));
      } catch (XPathSyntaxException e) {
        e.printStackTrace();
        TextView error = new TextView(context);
        error.setText("XPathParser Exception:  \"" + arguments.get(i) + "\"");
        addView(error);
        break;
      }

      if (xpr != null) {
        FormDef form = Collect.getInstance().getFormController().getFormDef();
        TreeElement mTreeElement =
            form.getMainInstance().resolveReference(prompt.getIndex().getReference());
        EvaluationContext ec =
            new EvaluationContext(form.getEvaluationContext(), mTreeElement.getRef());
        Object value = xpr.eval(form.getMainInstance(), ec);

        if (value == null) {
          nullArgs = true;
        } else {
          if (value instanceof XPathNodeset) {
            XPathNodeset xpn = (XPathNodeset) value;
            value = xpn.getValAt(0);
          }

          selectionArgs[i + 1] = value.toString();
        }
      }
    }

    File itemsetFile =
        new File(
            Collect.getInstance().getFormController().getMediaFolder().getAbsolutePath()
                + "/itemsets.csv");
    if (nullArgs) {
      // we can't try to query with null values else it blows up
      // so just leave the screen blank
      // TODO: put an error?
    } else if (itemsetFile.exists()) {
      ItemsetDbAdapter ida = new ItemsetDbAdapter();
      ida.open();

      // name of the itemset table for this form
      String pathHash = ItemsetDbAdapter.getMd5FromString(itemsetFile.getAbsolutePath());
      try {
        Cursor c = ida.query(pathHash, selection.toString(), selectionArgs);
        if (c != null) {
          c.move(-1);
          while (c.moveToNext()) {
            String label = "";
            String val = "";
            // try to get the value associated with the label:lang
            // string if that doen't exist, then just use label
            String lang = "";
            if (Collect.getInstance().getFormController().getLanguages() != null
                && Collect.getInstance().getFormController().getLanguages().length > 0) {
              lang = Collect.getInstance().getFormController().getLanguage();
            }

            // apparently you only need the double quotes in the
            // column name when creating the column with a :
            // included
            String labelLang = "label" + "::" + lang;
            int langCol = c.getColumnIndex(labelLang);
            if (langCol == -1) {
              label = c.getString(c.getColumnIndex("label"));
            } else {
              label = c.getString(c.getColumnIndex(labelLang));
            }

            // the actual value is stored in name
            val = c.getString(c.getColumnIndex("name"));
            mAnswers.put(label, val);

            RadioButton rb = new RadioButton(context);
            rb.setOnCheckedChangeListener(this);
            rb.setText(label);
            rb.setTextSize(mAnswerFontsize);
            mButtons.addView(rb);
            // have to add it to the radiogroup before checking it,
            // else it lets two buttons be checked...
            if (currentAnswer != null && val.compareTo(currentAnswer) == 0) {
              rb.setChecked(true);
            }
          }
          c.close();
        }
      } finally {
        ida.close();
      }

      addView(mButtons);
    } else {
      TextView error = new TextView(context);
      error.setText(getContext().getString(R.string.file_missing, itemsetFile.getAbsolutePath()));
      addView(error);
    }
  }