Esempio n. 1
0
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    /*
     * This method is executed for each item in the list in order to populate it with related data.
     * */
    View row = convertView;
    Answer answer = answers.get(position);
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (answer != null) {
      row = inflater.inflate(R.layout.answer_row, parent, false);
      TextView answerBody = (TextView) row.findViewById(R.id.answerBody);

      int colorPos = position % colors.length;
      // The answer accepted as the Correct answer has a green background
      if (answer.is_accepted()) {
        row.setBackgroundColor(Color.parseColor("#59F059"));
        answerBody.setText(Html.fromHtml("<b>Accepted Answer</b><br/>" + answer.getBody()));
      } else {
        row.setBackgroundColor(Color.parseColor(colors[colorPos]));
        answerBody.setText(Html.fromHtml(answer.getBody()));
      }

      TextView answerOwner = (TextView) row.findViewById(R.id.answerBodyOwner);
      answerOwner.setText(
          Html.fromHtml(
              "answered: "
                  + answer.getCreation_date()
                  + "<br/>By: "
                  + answer.getOwner().getDisplay_name()));

      // display comments related to each answer if they exist.
      // In case they do not exist, hide the widgets used to display comments.
      if (answer.getComment_count() > 0) {
        TextView comments = (TextView) row.findViewById(R.id.answerComments);
        String comStr = "<b>Comments:</b><br/>";
        ArrayList<Comment> coms = answer.getComments();
        for (int i = 0; i < coms.size(); i++) {
          Comment c = coms.get(i);
          comStr +=
              (i + 1)
                  + ". "
                  + c.getBody()
                  + " <br/> By: "
                  + c.getOwner().getDisplay_name()
                  + " - "
                  + c.getCreation_date()
                  + "<br/><br/>";
        }
        comments.setText(Html.fromHtml(comStr));
      } else {
        TextView comments = (TextView) row.findViewById(R.id.answerComments);
        comments.setVisibility(View.INVISIBLE);
        View viewComments1 = row.findViewById(R.id.viewComments1);
        viewComments1.setVisibility(View.INVISIBLE);
        View viewComments2 = row.findViewById(R.id.viewComments2);
        viewComments2.setVisibility(View.INVISIBLE);
      }
    }

    return row;
  }