Android Open Source - EasyVote Question Detail Fragment

From Project

Back to project page EasyVote.


The source code is released under:

Apache License

If you think the Android project EasyVote listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

//from ww w.ja  va 2 s .  c o m
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.backendless.Backendless;
import com.backendless.BackendlessCollection;
import com.backendless.async.callback.AsyncCallback;
import com.backendless.exceptions.BackendlessFault;
import com.backendless.persistence.BackendlessDataQuery;
import com.jjoe64.graphview.BarGraphView;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphView.GraphViewData;
import com.jjoe64.graphview.GraphViewDataInterface;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.GraphViewSeries.GraphViewSeriesStyle;
import com.jjoe64.graphview.ValueDependentColor;

 * A fragment representing a single Question detail screen. This fragment is
 * either contained in a {@link QuestionListActivity} in two-pane mode (on
 * tablets) or a {@link QuestionDetailActivity} on handsets.
public class QuestionDetailFragment extends Fragment {
   * The fragment argument representing the item ID that this fragment
   * represents.
  public static final String ARG_ITEM_ID = "item_id";

  private View mRootView;
  private Question mItem;

   * Mandatory empty constructor for the fragment manager to instantiate the
   * fragment (e.g. upon screen orientation changes).
  public QuestionDetailFragment() {

  public void onCreate(Bundle savedInstanceState) {

    if (getArguments().containsKey(ARG_ITEM_ID)) {
      // Load the dummy content specified by the fragment
      // arguments. In a real-world scenario, use a Loader
      // to load content from a content provider.
      String idItem = getArguments().getString(  ARG_ITEM_ID);

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    mRootView = inflater.inflate(R.layout.fragment_question_detail,
        container, false);
    String idItem = getArguments().getString(ARG_ITEM_ID);
    DataSession<Question> data = new QuestionData(getActivity());
    mItem = data.findById(idItem);
    if (mItem != null) {      
      ((TextView) mRootView.findViewById(;


    return mRootView;

  private void retrieveAnswers(String id) {
    String whereClause = "questionId = "+id;
    BackendlessDataQuery dataQuery = new BackendlessDataQuery();
    dataQuery.setWhereClause( whereClause );
    Backendless.Persistence.of( AnswerQuestion.class ).find( dataQuery, 
                new AsyncCallback<BackendlessCollection<AnswerQuestion>>(){

          public void handleFault(BackendlessFault fault) {          
            Toast.makeText(getActivity(), R.string.toast_err_retrieving_data, Toast.LENGTH_LONG).show();

          public void handleResponse(  BackendlessCollection<AnswerQuestion> response) {
          }} );

  protected void updatedAnswers(List<AnswerQuestion> data) {

    Context ctx = this.getActivity().getBaseContext();
    GraphViewSeriesStyle seriesStyle = new GraphViewSeriesStyle();
    seriesStyle.setValueDependentColor(new ValueDependentColor() {
      public int get(GraphViewDataInterface data) {
        // the higher the more red
        return Color.rgb((int)(150+((data.getY()/3)*100)), (int)(150-((data.getY()/3)*150)), (int)(150-((data.getY()/3)*150)));
    GraphViewData[] viewData = createViewData(data);
    GraphViewSeries dataSeries = new GraphViewSeries("Estadisticas", seriesStyle,viewData);
    GraphView graphView = new BarGraphView(
          ctx // context
          , mItem.getContent() // heading
    graphView.setHorizontalLabels(new String[] {"S", "No", "Blanco"});

      graphView.addSeries(dataSeries); // data
      LinearLayout panelGraphics = (LinearLayout) mRootView.findViewById(;

  private GraphViewData[] createViewData(List<AnswerQuestion> data) {
    GraphViewData[] viewData = new GraphViewData[data.size()];
    int[] grupos = new int[4];
    for(AnswerQuestion answer : data){
          grupos[0] = answer.getPoints()+ grupos[0]; 
          grupos[1] = answer.getPoints()+ grupos[1]; 
        if("En Blanco".equalsIgnoreCase(answer.getAnswer())){
          grupos[2] = answer.getPoints()+ grupos[2]; 
    return new GraphViewData[] {
            new GraphViewData(1, grupos[0])
          , new GraphViewData(2, grupos[1])
          , new GraphViewData(3, grupos[2]) };
            // another frequency

Java Source Code List