Android Open Source - SurveySDK_android Poll Selection Table






From Project

Back to project page SurveySDK_android.

License

The source code is released under:

Apache License

If you think the Android project SurveySDK_android 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

package com.survey.android.custom_widgets;
//from  w  w w .j  av a 2 s . com
import java.util.LinkedList;
import java.util.List;

import com.survey.android.R;
import com.survey.android.model.AnswerModel;
import com.survey.android.util.ConstantData;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PollSelectionTable extends LinearLayout {

  public static final int SINGLE_SELECT = 0;
  public static final int MULTIPLE_SELECT = 1;

  private String question_id;
  @SuppressWarnings("unused")
  private List<String> _id;
  private List<String> label;
  private List<String> value;
  private List<Boolean> checked;
  private List<Boolean> mutually_exclusive;
  private int type;

  public PollSelectionTable(Context context) {
    super(context);
  }

  public PollSelectionTable(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public void initializeItems(String question_id, int type, List<String> ids,
      List<String> labels, List<String> values, List<Boolean> exclusive) {
    this.question_id = question_id;
    this.type = type;
    this._id = ids;
    this.label = labels;
    this.value = values;
    this.mutually_exclusive = exclusive;
    this.checked = new LinkedList<Boolean>();
    LayoutInflater inflater = (LayoutInflater) this.getContext()
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (int i = 0; i < values.size(); i++) {
      final int id = i;
      this.checked.add(false);
      final View row = inflater.inflate(R.layout.q_list_row_text, null);

      TextView txtChoice = (TextView) row.findViewById(R.id.txtChoice);
      txtChoice.setText(label.get(id));

      row.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
          boolean temp = checked.get(id);
          if (PollSelectionTable.this.type == SINGLE_SELECT) {
            for (int j = 0; j < checked.size(); j++) {
              checked.set(j, false);
              repaintRow(j, false);
                
//              View tempView = PollSelectionTable.this
//                  .getChildAt(j);
//              TextView txtChoice = (TextView) tempView
//                  .findViewById(R.id.txtChoice);
//              CheckBox cbTemp = (CheckBox) tempView
//                  .findViewById(R.id.chk);
//              cbTemp.setChecked(false);
//              txtChoice.setTextColor(getResources().getColor(R.color.gray));
            }
          }
          else{
            boolean exclusive=mutually_exclusive.get(id);
            if(exclusive && !temp){
              clearAll();
            }
            else{
              boolean flag=true;
              int k=0;
              while(k<checked.size() && flag){
                if(checked.get(k) && mutually_exclusive.get(k)){
                  clearAll();
                  flag=false;
                }
                k++;
              }
            }
          }
          checked.set(id, !temp);
          CheckBox cbTemp = (CheckBox) row.findViewById(R.id.chk);
          TextView txtTemp = (TextView) row
              .findViewById(R.id.txtChoice);

          txtTemp.setTextColor(!temp ? getResources().getColor(R.color.green_slider) :getResources().getColor( R.color.gray));
          cbTemp.setChecked(!temp);
        }
      });
      this.addView(row);
    }
  }

  public void initializeItems(int type, List<String> ids,
      List<String> labels, List<String> values) {
    this.type = type;
    this._id = ids;
    this.label = labels;
    this.value = values;
    this.checked = new LinkedList<Boolean>();
    LayoutInflater inflater = (LayoutInflater) this.getContext()
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (int i = 0; i < values.size(); i++) {
      final int id = i;
      this.checked.add(false);
      final View row = inflater.inflate(R.layout.q_list_row_text, null);

      TextView txtChoice = (TextView) row.findViewById(R.id.txtChoice);
      txtChoice.setText(label.get(id));

      row.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
          boolean temp = checked.get(id);
          if (PollSelectionTable.this.type == SINGLE_SELECT) {
            for (int j = 0; j < checked.size(); j++) {
              checked.set(j, false);
              repaintRow(j, false);
//              View tempView = PollSelectionTable.this
//                  .getChildAt(j);
//              CheckBox cbTemp = (CheckBox) tempView
//                  .findViewById(R.id.chk);
//              TextView txtChoice = (TextView) tempView
//                  .findViewById(R.id.txtChoice);
//
//              cbTemp.setChecked(false);
//              txtChoice.setTextColor(getResources().getColor(R.color.gray));
            }
          }
          else{
            boolean exclusive=mutually_exclusive.get(id);
            if(exclusive && !temp){
              clearAll();
            }
            else{
              boolean flag=true;
              int k=0;
              while(k<checked.size() && flag){
                if(checked.get(k) && mutually_exclusive.get(k)){
                  clearAll();
                  flag=false;
                }
                k++;
              }
            }
          }
          checked.set(id, !temp);
          CheckBox cbTemp = (CheckBox) row.findViewById(R.id.chk);
          TextView txtTemp = (TextView) row
              .findViewById(R.id.txtChoice);

          txtTemp.setTextColor(!temp ? getResources().getColor(R.color.green_slider) : getResources().getColor(R.color.gray));
          cbTemp.setChecked(!temp);
        }
      });

      this.addView(row);
    }
  }

  /**
   * Returns boolean which indicates if there is at least one row marked
   * 
   * @return
   */
  public boolean isAnswered() {
    for (boolean b : this.checked) {
      if (b)
        return true;
    }
    return false;
  }

  /**
   * Returns list of selected indexes
   * 
   * @return  
   */
  private List<Integer> getSelectedIndexes() {
    List<Integer> result = new LinkedList<Integer>();
    for (int i = 0; i < this.checked.size(); i++) {
      if (this.checked.get(i))
        result.add(i);
    }
    return result;
  }

  /**
   * Returns selected answer - used in single select question
   * 
   * @return
   */
  public String getSelectedAnswer() {
    List<Integer> selected = getSelectedIndexes();
    return this.value.get(selected.get(0));
  }

  /**
   * Returns list of selected answers - used in multi select question
   * 
   * @return
   */
  public List<AnswerModel> getSelectedAnswers() {
    List<AnswerModel> result = new LinkedList<AnswerModel>();
    for (int i = 0; i < checked.size(); i++) {
      if (checked.get(i)) {
        result.add(new AnswerModel(question_id,
            ConstantData.RESPONSE_TYPE_MULTIPLE_SELECT, this.value
                .get(i)));
      }
    }
    return result;
  }

  /**
   * 
   * @param answers
   */
  public void setSelectedAnswers(List<AnswerModel> answers) {
    if (answers != null) {
      for (AnswerModel answer : answers) {
        boolean tempContinue = true;
        for (int i = 0; i < value.size() && tempContinue; i++) {
          if (value.get(i).equals(answer.getAnswer())) {
            this.checked.set(i, true);
            tempContinue = false;
            repaintRow(i,/*this.checked.get(i)*/true);
          }
        }
      }
      invalidate();
    }
  }

  /**
   * Clear list
   */
  public void clearAll() {
    for (int i = 0; i < value.size(); i++) {
      boolean tempChecked=this.checked.get(i);
      if(tempChecked){
        repaintRow(i, false);
//        View temp=PollSelectionTable.this.getChildAt(i);
//        CheckBox cbTemp=(CheckBox)temp.findViewById(R.id.chk);
//        TextView txtTemp=(TextView)temp.findViewById(R.id.txtChoice);
//        cbTemp.setChecked(false);
//        txtTemp.setTextColor(getResources().getColor(R.color.gray));
      }
      this.checked.set(i, false);
    }
    invalidate();
  }
  
  /**  
   * Repaints text and checkbox based on passed parameters: index of row and value ( tru, false )
   * @param index
   * @param value
   */
  private void repaintRow(int index, boolean value){
    try{
      View temp=PollSelectionTable.this.getChildAt(index);
      CheckBox cbTemp=(CheckBox)temp.findViewById(R.id.chk);
      TextView txtTemp=(TextView)temp.findViewById(R.id.txtChoice);
      cbTemp.setChecked(value);
      txtTemp.setTextColor(value?getResources().getColor(R.color.green_slider):getResources().getColor(R.color.gray));
    }
    catch(Exception e){
      e.printStackTrace();
    }
  }
}




Java Source Code List

android.UnusedStub.java
com.google.android.gms.BuildConfig.java
com.google.android.gms.BuildConfig.java
com.survey.android.UnusedStub.java
com.survey.android.c2dm.C2DMRegistrationReceiver.java
com.survey.android.c2dm.C2DMTokenRefresher.java
com.survey.android.c2dm.GcmBroadcastReceiver.java
com.survey.android.c2dm.GcmNotificationReceiver.java
com.survey.android.c2dm.GcmRegistrationService.java
com.survey.android.c2dm.RegService.java
com.survey.android.common.PlacesAutoCompleteAdapter.java
com.survey.android.common.Themes.java
com.survey.android.containers.AppContainer.java
com.survey.android.containers.PollContainer.java
com.survey.android.custom_widgets.FontTextView.java
com.survey.android.custom_widgets.PollRatingsScaleLabeled.java
com.survey.android.custom_widgets.PollRatings.java
com.survey.android.custom_widgets.PollSelectionTable.java
com.survey.android.db.SerializationHelper.java
com.survey.android.db.SerializationManager.java
com.survey.android.fragment.AudioRecorderFragment.java
com.survey.android.geofence.GeofenceRemover.java
com.survey.android.geofence.GeofenceRequester.java
com.survey.android.geofence.GeofenceUtils.java
com.survey.android.geofence.LocationServiceErrorMessages.java
com.survey.android.geofence.ReceiveTransitionsIntentService.java
com.survey.android.geofence.SimpleGeofenceStore.java
com.survey.android.geofence.SimpleGeofence.java
com.survey.android.model.AnswerModel.java
com.survey.android.model.CategoryModel.java
com.survey.android.model.CurrentSectionModel.java
com.survey.android.model.Prefs.java
com.survey.android.model.QuestionModel.java
com.survey.android.model.ResponseModel.java
com.survey.android.model.SurveyModel.java
com.survey.android.model.UserModel.java
com.survey.android.services.BackgroundUploader.java
com.survey.android.services.DataBroadcastReceiver.java
com.survey.android.services.DeviceStartUpReceiver.java
com.survey.android.services.GeoSurveyPollService.java
com.survey.android.services.LocationTesterService.java
com.survey.android.services.ReferrerCatcher.java
com.survey.android.session.Configuration.java
com.survey.android.session.Session.java
com.survey.android.util.Base64.java
com.survey.android.util.ConstantData.java
com.survey.android.util.GeoPush.java
com.survey.android.util.GeoTriggerBroadcastReceiver.java
com.survey.android.util.GeoTrigger.java
com.survey.android.util.LocationLog.java
com.survey.android.util.Log.java
com.survey.android.util.StrToIntMap.java
com.survey.android.util.StrToStrMap.java
com.survey.android.util.Toiler.java
com.survey.android.util.WhiteLabel.java
com.survey.android.view.Dashboard.java
com.survey.android.view.Gallery.java
com.survey.android.view.LocalizedFragmentActivity.java
com.survey.android.view.Main.java
com.survey.android.view.Notification.java
com.survey.android.view.Question.java
com.survey.android.view.Survey.java
com.survey.android.view.ThemeCustomizer.java
com.survey.android.view.themed.DashboardThemed.java
com.survey.android.view.themed.MainThemed.java
com.survey.android.view.themed.NotificationThemed.java
com.survey.android.view.themed.QuestionThemed.java
com.survey.android.view.themed.SurveyThemed.java
com.survey.android.webclient.HttpRequest.java
com.survey.android.webclient.HttpsClient.java
com.survey.android.webclient.RestClient.java
com.survey.android.webclient.SurveyHttpClient.java
com.survey.android.webclient.SurveyRequest.java
com.survey.android.webclient.SurveySSLSocketFactory.java
com.survey.android.webclient.SurveyX509TrustManager.java
com.survey.android.widget.Widget.java
com.survey.androiddemo.AppContainer.java
com.survey.androiddemo.LoginActivity.java
com.survey.androiddemo.MainActivity.java
com.survey.androiddemo.SDKConfigSettings.java