Advertisement
Guest User

Untitled

a guest
Aug 28th, 2012
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 35.75 KB | None | 0 0
  1. package com.itse.htsurvey;
  2.  
  3. import com.google.gson.Gson;
  4.  
  5. import android.app.Activity;
  6. import android.app.ProgressDialog;
  7. import android.content.Intent;
  8. import android.content.SharedPreferences;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.view.Menu;
  12. import android.view.MenuInflater;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.widget.AdapterView;
  16. import android.widget.ArrayAdapter;
  17. import android.widget.CheckBox;
  18. import android.widget.CompoundButton;
  19. import android.widget.CompoundButton.OnCheckedChangeListener;
  20. import android.widget.EditText;
  21. import android.widget.LinearLayout;
  22. import android.widget.RelativeLayout;
  23. import android.widget.ScrollView;
  24. import android.widget.TextView;
  25. import android.widget.Toast;
  26. import android.widget.AdapterView.OnItemSelectedListener;
  27.  
  28. public class Question2Page2Activity extends Activity {
  29.    
  30.     private static final Integer nextRequestCode = 10000;
  31.     private String user_type;
  32.     private QuestionnaireDataSet qDataSet;
  33.     private ProgressDialog ProcessingDialog;
  34.    
  35.     /** Called when the activity is first created. */
  36.     @Override
  37.     public void onCreate(Bundle savedInstanceState) {
  38.         super.onCreate(savedInstanceState);
  39.         setContentView(R.layout.section2_page2);
  40.        
  41.         //retrieve reference to object of class
  42.         Intent i = getIntent();
  43.         qDataSet = (QuestionnaireDataSet) i.getSerializableExtra("qDataSet");
  44.        
  45.         setupQ2_7();
  46.         setupQ2_8();
  47.         setupQ2_9();
  48.         setupQ2_10();
  49.         setupQ2_11();
  50.         setupQ2_11_1();
  51.        
  52.         //disable auto focus on scroll in scrollview
  53.         ScrollView sv = (ScrollView) findViewById(R.id.scrollView);
  54.         ScrollViewFocus svf = new ScrollViewFocus(sv);
  55.         svf.noScroll();
  56.         //scroll view back to top
  57.         svf.scrollToTop();
  58.        
  59.         //restore user_type setting
  60.         SharedPreferences settings = getSharedPreferences("user_type", MODE_PRIVATE);
  61.         user_type = settings.getString("user_type", null);        
  62.     }
  63.    
  64.     //------------------------------------------------------------------------------------------------------
  65.     //repopulate fields with saved data
  66.     //------------------------------------------------------------------------------------------------------       
  67.     protected void onStart() {
  68.         super.onStart();
  69.         populateFields();
  70.     }      
  71.    
  72.     //------------------------------------------------------------------------------------------------------
  73.     //show menu
  74.     //------------------------------------------------------------------------------------------------------
  75.     @Override
  76.     public boolean onCreateOptionsMenu(Menu menu) {
  77.         MenuInflater inflater = getMenuInflater();
  78.         if (user_type.equals("interviewer"))
  79.             inflater.inflate(R.menu.menu, menu);
  80.         else
  81.             inflater.inflate(R.menu.menu_supervisor, menu);
  82.         return true;
  83.     }
  84.    
  85.     //------------------------------------------------------------------------------------------------------
  86.     //handle menu item selection
  87.     //------------------------------------------------------------------------------------------------------
  88.     @Override
  89.     public boolean onOptionsItemSelected(MenuItem item) {
  90.        
  91.         actionBarActivities abAct = new actionBarActivities(this, this);
  92.         Gson gson = new Gson();
  93.         String JSONstring;
  94.         ProgressDialog ProcessingDialog = null;
  95.        
  96.         switch (item.getItemId()) {
  97.             case R.id.menu_sign_off:
  98.                 abAct.confirmSignOff(qDataSet.getqID());
  99.                 return true;
  100.             case R.id.menu_save:  
  101.                 //save entered data
  102.                 saveEnteredData();             
  103.                 JSONstring = gson.toJson(qDataSet);
  104.                 abAct.saveQuestionnaire(JSONstring, qDataSet.getqID());
  105.                 return true;
  106.             case R.id.menu_submit:      
  107.                 saveEnteredData();
  108.                 if (qDataSet.getQ6_3_3_2() != null) {
  109.                     if (qDataSet.getPeopleNum() > 0 && qDataSet.getPeopleNum() == Integer.parseInt(qDataSet.getQ1_2())) {                  
  110.                         JSONstring = gson.toJson(qDataSet);
  111.                         Log.d("Gson", "JSON String: "+JSONstring);
  112.                         QuestionnaireUpload qu = new QuestionnaireUpload(this, this);
  113.                         qu.upload(JSONstring, ProcessingDialog, getString(R.string.sendQuestionnairePHPurl));
  114.                     }
  115.                     else
  116.                         Toast.makeText(this, "First complete the questionnaire for all household members.", Toast.LENGTH_SHORT).show();
  117.                 }
  118.                 else
  119.                     Toast.makeText(this, "First complete the questionnaire before submitting it.", Toast.LENGTH_SHORT).show();
  120.                 return true;
  121.             case R.id.menu_close:    
  122.                 abAct.confirmClose(qDataSet);
  123.                 return true;
  124.             case R.id.menu_exit:    
  125.                 abAct.confirmExit(qDataSet);
  126.                 return true;
  127.             default:
  128.                 return super.onOptionsItemSelected(item);
  129.         }          
  130.        
  131.     }          
  132.    
  133.     //-------------------------------------------------------------------------------------------------------
  134.     //setup spinners for question 2.7
  135.     //-------------------------------------------------------------------------------------------------------
  136.     private void setupQ2_7() {
  137.        
  138.         //add custom view to layout for every person
  139.         LinearLayout insertPoint = (LinearLayout) findViewById(R.id.ll2_7);    
  140.        
  141.         for (int i = 0; i < qDataSet.getPeopleNum(); i++) {
  142.            
  143.             //inflate custom1 view
  144.             View v =  getLayoutInflater().inflate(R.layout.custom1, insertPoint, false);
  145.                        
  146.             //add name and surname to textview
  147.             TextView tvName = (TextView) v.findViewById(R.id.textView1);
  148.             tvName.setText(qDataSet.getPerson(i).getFirstName() + " " + qDataSet.getPerson(i).getSurname());
  149.            
  150.             //add details to spinner
  151.             NoDefaultSpinner s1 = (NoDefaultSpinner) v.findViewById(R.id.spinner1);
  152.             s1.setPrompt(getString(R.string.question2_7_spinnerTitle));
  153.             s1.setId(i);           
  154.             //set spinner width
  155.             LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) s1.getLayoutParams();
  156.             lp.width = 150;
  157.             s1.setLayoutParams(lp);        
  158.             ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.question2_7_spinner, android.R.layout.simple_spinner_item);
  159.             adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  160.             s1.setAdapter(adapter);
  161.             //add click listener to spinner
  162.             final LinearLayout ll2_8 = (LinearLayout) findViewById(R.id.ll2_8);
  163.             final LinearLayout ll2_9 = (LinearLayout) findViewById(R.id.ll2_9);
  164.             s1.setOnItemSelectedListener(new OnItemSelectedListener() {
  165.                 public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {                  
  166.                     if (position == (parentView.getCount()-1)) {
  167.                         ll2_8.getChildAt(parentView.getId()).setVisibility(View.VISIBLE);
  168.                         //add enable events for next question
  169.                         ll2_8.getChildAt(parentView.getId()).findViewById(R.id.tl1).findViewById(R.id.tr1).findViewById(parentView.getId()).setEnabled(true);  
  170.                         ll2_8.getChildAt(parentView.getId()).findViewById(R.id.tl1).findViewById(R.id.tr1).findViewById(parentView.getId() + 100).setEnabled(true);
  171.                         ll2_8.getChildAt(parentView.getId()).findViewById(R.id.tl1).findViewById(R.id.tr2).findViewById(parentView.getId() + 200).setEnabled(true);
  172.                         ll2_8.getChildAt(parentView.getId()).findViewById(R.id.tl1).findViewById(R.id.tr2).findViewById(parentView.getId() + 300).setEnabled(true);
  173.                         ll2_8.getChildAt(parentView.getId()).findViewById(R.id.tl1).findViewById(R.id.tr3).findViewById(parentView.getId() + 400).setEnabled(true);
  174.                         ll2_8.getChildAt(parentView.getId()).findViewById(R.id.tl1).findViewById(R.id.tr3).findViewById(parentView.getId() + 500).setEnabled(true);
  175.                         ll2_8.getChildAt(parentView.getId()).findViewById(R.id.tl1).findViewById(R.id.tr4).findViewById(parentView.getId() + 600).setEnabled(true);
  176.                         ll2_8.getChildAt(parentView.getId()).findViewById(R.id.tl1).findViewById(R.id.tr4).findViewById(parentView.getId() + 700).setEnabled(true);
  177.                     }
  178.                     else {
  179.                         ll2_8.getChildAt(parentView.getId()).setVisibility(View.GONE);
  180.                         //add enable events for next question
  181.                         ll2_9.getChildAt(parentView.getId()).findViewById(parentView.getId()).setEnabled(true);
  182.                     }
  183.                    
  184.                    
  185.                 }
  186.                 public void onNothingSelected(AdapterView<?> arg0) {
  187.                     // TODO Auto-generated method stub                 
  188.                 }              
  189.             });
  190.            
  191.             //add inflated view to linear layout
  192.             insertPoint.addView(v);            
  193.            
  194.         }      
  195.     }
  196.    
  197.     //-------------------------------------------------------------------------------------------------------
  198.     //setup spinners for question 2.8
  199.     //-------------------------------------------------------------------------------------------------------
  200.     private void setupQ2_8() {
  201.        
  202.         //add custom view to layout for every person
  203.         LinearLayout insertPoint = (LinearLayout) findViewById(R.id.ll2_8);
  204.        
  205.         for (int i = 0; i < qDataSet.getPeopleNum(); i++) {
  206.            
  207.             //inflate custom1 view
  208.             View v =  getLayoutInflater().inflate(R.layout.custom13, insertPoint, false);
  209.            
  210.             //add name and surname to textview         
  211.             TextView tvName = (TextView) v.findViewById(R.id.textView1);
  212.             tvName.setText(qDataSet.getPerson(i).getFirstName() + " " + qDataSet.getPerson(i).getSurname());
  213.            
  214.             //add details to check boxes
  215.             CheckBox cb1 = (CheckBox) v.findViewById(R.id.checkBox1);
  216.             CheckBox cb2 = (CheckBox) v.findViewById(R.id.checkBox2);
  217.             CheckBox cb3 = (CheckBox) v.findViewById(R.id.checkBox3);
  218.             CheckBox cb4 = (CheckBox) v.findViewById(R.id.checkBox4);
  219.             CheckBox cb5 = (CheckBox) v.findViewById(R.id.checkBox5);
  220.             CheckBox cb6 = (CheckBox) v.findViewById(R.id.checkBox6);
  221.             CheckBox cb7 = (CheckBox) v.findViewById(R.id.checkBox7);
  222.             CheckBox cb8 = (CheckBox) v.findViewById(R.id.checkBox8);
  223.            
  224.             cb1.setId(i);
  225.             cb2.setId(i + 100);
  226.             cb3.setId(i + 200);
  227.             cb4.setId(i + 300);
  228.             cb5.setId(i + 400);
  229.             cb6.setId(i + 500);
  230.             cb7.setId(i + 600);
  231.             cb8.setId(i + 700);
  232.            
  233.             //add id to edittext
  234.             final EditText et1 = (EditText) v.findViewById(R.id.editText1);
  235.             et1.setId(i + 800);
  236.            
  237.             //add click listener to check boxes
  238.             final LinearLayout ll2_9 = (LinearLayout) findViewById(R.id.ll2_9);        
  239.             OnCheckedChangeListener cbListener = new OnCheckedChangeListener() {               
  240.                 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  241.                     //add enable events for next question
  242.                     CheckBox cb = (CheckBox) buttonView;
  243.                     ll2_9.getChildAt(cb.getId() % 100).findViewById(cb.getId() % 100).setEnabled(true);
  244.                 }
  245.             };
  246.             cb1.setOnCheckedChangeListener(cbListener);
  247.             cb2.setOnCheckedChangeListener(cbListener);
  248.             cb3.setOnCheckedChangeListener(cbListener);
  249.             cb4.setOnCheckedChangeListener(cbListener);
  250.             cb5.setOnCheckedChangeListener(cbListener);
  251.             cb6.setOnCheckedChangeListener(cbListener);
  252.             cb7.setOnCheckedChangeListener(cbListener);
  253.             //click listener for Other
  254.             cb8.setOnCheckedChangeListener(new OnCheckedChangeListener() {             
  255.                 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  256.                     //make visible specify other edittext
  257.                     CheckBox cb = (CheckBox) buttonView;
  258.                     if (isChecked)
  259.                         et1.setVisibility(View.VISIBLE);
  260.                     else {
  261.                         et1.setVisibility(View.GONE);
  262.                         et1.setText("");
  263.                     }              
  264.                     //add enable events for next question                  
  265.                     ll2_9.getChildAt(cb.getId() % 100).findViewById(cb.getId() % 100).setEnabled(true);                
  266.                 }
  267.             });        
  268.            
  269.             //add inflated view to linear layout
  270.             v.setVisibility(View.GONE);
  271.             insertPoint.addView(v);
  272.            
  273.             //disable views
  274.             cb1.setEnabled(false);
  275.             cb2.setEnabled(false);
  276.             cb3.setEnabled(false);
  277.             cb4.setEnabled(false);
  278.             cb5.setEnabled(false);
  279.             cb6.setEnabled(false);
  280.             cb7.setEnabled(false);
  281.             cb8.setEnabled(false);
  282.         }      
  283.     }
  284.    
  285.     //-------------------------------------------------------------------------------------------------------
  286.     //setup spinners for question 2.9
  287.     //-------------------------------------------------------------------------------------------------------
  288.     private void setupQ2_9() {
  289.        
  290.         //add custom view to layout for every person
  291.         LinearLayout insertPoint = (LinearLayout) findViewById(R.id.ll2_9);
  292.        
  293.         for (int i = 0; i < qDataSet.getPeopleNum(); i++) {
  294.            
  295.             //inflate custom1 view
  296.             View v =  getLayoutInflater().inflate(R.layout.custom2, insertPoint, false);
  297.            
  298.             //add name and surname to textview         
  299.             TextView tvName = (TextView) v.findViewById(R.id.textView1);
  300.             tvName.setText(qDataSet.getPerson(i).getFirstName() + " " + qDataSet.getPerson(i).getSurname());
  301.            
  302.             //add details to spinner
  303.             NoDefaultSpinner s1 = (NoDefaultSpinner) v.findViewById(R.id.spinner1);
  304.             s1.setPrompt(getString(R.string.question2_9_spinnerTitle));
  305.             s1.setId(i);
  306.             ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.question2_9_spinner, android.R.layout.simple_spinner_item);
  307.             adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  308.             s1.setAdapter(adapter);
  309.             //add id to edittext
  310.             final EditText et1 = (EditText) v.findViewById(R.id.editText1);
  311.             et1.setId(i + 100);
  312.             //add click listener to spinner        
  313.             final LinearLayout ll2_10 = (LinearLayout) findViewById(R.id.ll2_10);
  314.             s1.setOnItemSelectedListener(new OnItemSelectedListener() {
  315.                 public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {                  
  316.                     if ((position == (parentView.getCount()-1)) || (position == (parentView.getCount()-2)))
  317.                         et1.setVisibility(View.VISIBLE);
  318.                     else {
  319.                         et1.setVisibility(View.INVISIBLE);
  320.                         et1.setText("");
  321.                     }
  322.                     //add enable events for next question
  323.                     ll2_10.getChildAt(parentView.getId()).findViewById(parentView.getId()).setEnabled(true);
  324.                 }
  325.                 public void onNothingSelected(AdapterView<?> arg0) {
  326.                     // TODO Auto-generated method stub                 
  327.                 }              
  328.             });
  329.            
  330.             //add inflated view to linear layout
  331.             insertPoint.addView(v);
  332.            
  333.             //disable views
  334.             s1.setEnabled(false);
  335.         }      
  336.     }
  337.    
  338.     //-------------------------------------------------------------------------------------------------------
  339.     //setup spinners for question 2.10
  340.     //-------------------------------------------------------------------------------------------------------
  341.     private void setupQ2_10() {
  342.        
  343.         //add custom view to layout for every person
  344.         LinearLayout insertPoint = (LinearLayout) findViewById(R.id.ll2_10);
  345.        
  346.         for (int i = 0; i < qDataSet.getPeopleNum(); i++) {
  347.            
  348.             //inflate custom1 view
  349.             View v =  getLayoutInflater().inflate(R.layout.custom2, insertPoint, false);
  350.            
  351.             //add name and surname to textview         
  352.             TextView tvName = (TextView) v.findViewById(R.id.textView1);
  353.             tvName.setText(qDataSet.getPerson(i).getFirstName() + " " + qDataSet.getPerson(i).getSurname());
  354.            
  355.             //add details to spinner
  356.             NoDefaultSpinner s1 = (NoDefaultSpinner) v.findViewById(R.id.spinner1);
  357.             s1.setPrompt(getString(R.string.question2_10_spinnerTitle));
  358.             s1.setId(i);
  359.             ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.question2_10_spinner, android.R.layout.simple_spinner_item);
  360.             adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  361.             s1.setAdapter(adapter);
  362.             //add id to edittext
  363.             final EditText et1 = (EditText) v.findViewById(R.id.editText1);
  364.             et1.setId(i + 100);
  365.             //add click listener to spinner        
  366.             final LinearLayout ll2_11 = (LinearLayout) findViewById(R.id.ll2_11);
  367.             s1.setOnItemSelectedListener(new OnItemSelectedListener() {
  368.                 public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {                  
  369.                     if (position == (parentView.getCount()-1))
  370.                         et1.setVisibility(View.VISIBLE);
  371.                     else {
  372.                         et1.setVisibility(View.INVISIBLE);
  373.                         et1.setText("");
  374.                     }
  375.                     //add enable events for next question
  376.                     ll2_11.getChildAt(parentView.getId()).findViewById(parentView.getId()).setEnabled(true);
  377.                 }
  378.                 public void onNothingSelected(AdapterView<?> arg0) {
  379.                     // TODO Auto-generated method stub                 
  380.                 }              
  381.             });
  382.            
  383.             //add inflated view to linear layout
  384.             insertPoint.addView(v);
  385.            
  386.             //disable views
  387.             s1.setEnabled(false);
  388.         }      
  389.     }
  390.    
  391.     //-------------------------------------------------------------------------------------------------------
  392.     //setup spinners for question 2.11
  393.     //-------------------------------------------------------------------------------------------------------
  394.     private void setupQ2_11() {
  395.        
  396.         //add custom view to layout for every person
  397.         LinearLayout insertPoint = (LinearLayout) findViewById(R.id.ll2_11);
  398.        
  399.         for (int i = 0; i < qDataSet.getPeopleNum(); i++) {
  400.            
  401.             //inflate custom1 view
  402.             View v =  getLayoutInflater().inflate(R.layout.custom1, insertPoint, false);
  403.            
  404.             //add name and surname to textview
  405.             TextView tvName = (TextView) v.findViewById(R.id.textView1);
  406.             tvName.setText(qDataSet.getPerson(i).getFirstName() + " " + qDataSet.getPerson(i).getSurname());
  407.            
  408.             //add details to spinner
  409.             NoDefaultSpinner s1 = (NoDefaultSpinner) v.findViewById(R.id.spinner1);
  410.             s1.setPrompt(getString(R.string.question2_11_spinnerTitle));
  411.             s1.setId(i);           
  412.             //set spinner width
  413.             LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) s1.getLayoutParams();
  414.             lp.width = 150;
  415.             s1.setLayoutParams(lp);
  416.             ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.question2_11_spinner, android.R.layout.simple_spinner_item);
  417.             adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  418.             s1.setAdapter(adapter);
  419.             //add click listener to spinner
  420.             final LinearLayout ll2_11_1 = (LinearLayout) findViewById(R.id.ll2_11_1);
  421.             s1.setOnItemSelectedListener(new OnItemSelectedListener() {
  422.                 public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {                  
  423.                     if (position == (parentView.getCount()-1)) {
  424.                         ll2_11_1.getChildAt(parentView.getId()).setVisibility(View.VISIBLE);
  425.                         //add enable events for next question
  426.                         ll2_11_1.getChildAt(parentView.getId()).findViewById(R.id.tl1).findViewById(R.id.tr1).findViewById(parentView.getId()).setEnabled(true);   
  427.                         ll2_11_1.getChildAt(parentView.getId()).findViewById(R.id.tl1).findViewById(R.id.tr1).findViewById(parentView.getId() + 100).setEnabled(true); 
  428.                         ll2_11_1.getChildAt(parentView.getId()).findViewById(R.id.tl1).findViewById(R.id.tr2).findViewById(parentView.getId() + 200).setEnabled(true); 
  429.                         ll2_11_1.getChildAt(parentView.getId()).findViewById(R.id.tl1).findViewById(R.id.tr2).findViewById(parentView.getId() + 300).setEnabled(true); 
  430.                     }
  431.                     else
  432.                         ll2_11_1.getChildAt(parentView.getId()).setVisibility(View.GONE);                  
  433.                 }
  434.                 public void onNothingSelected(AdapterView<?> arg0) {
  435.                     // TODO Auto-generated method stub                 
  436.                 }              
  437.             });
  438.            
  439.             //add inflated view to linear layout
  440.             if (Integer.parseInt(qDataSet.getPerson(i).getAge()) < 18)
  441.                 v.setVisibility(View.GONE);
  442.             insertPoint.addView(v);
  443.            
  444.             //disable views
  445.             s1.setEnabled(false);
  446.         }      
  447.     }
  448.    
  449.     //-------------------------------------------------------------------------------------------------------
  450.     //setup spinners for question 2.11.1
  451.     //-------------------------------------------------------------------------------------------------------
  452.     private void setupQ2_11_1() {
  453.        
  454.         //add custom view to layout for every person
  455.         LinearLayout insertPoint = (LinearLayout) findViewById(R.id.ll2_11_1);
  456.        
  457.         for (int i = 0; i < qDataSet.getPeopleNum(); i++) {
  458.            
  459.             //inflate custom1 view
  460.             View v =  getLayoutInflater().inflate(R.layout.custom14, insertPoint, false);
  461.            
  462.             //add name and surname to textview         
  463.             TextView tvName = (TextView) v.findViewById(R.id.textView1);
  464.             tvName.setText(qDataSet.getPerson(i).getFirstName() + " " + qDataSet.getPerson(i).getSurname());
  465.            
  466.             //add details to check boxes
  467.             CheckBox cb1 = (CheckBox) v.findViewById(R.id.checkBox1);
  468.             CheckBox cb2 = (CheckBox) v.findViewById(R.id.checkBox2);
  469.             CheckBox cb3 = (CheckBox) v.findViewById(R.id.checkBox3);
  470.             CheckBox cb4 = (CheckBox) v.findViewById(R.id.checkBox4);
  471.            
  472.             cb1.setId(i);
  473.             cb2.setId(i + 100);
  474.             cb3.setId(i + 200);
  475.             cb4.setId(i + 300);
  476.            
  477.             //add id to edittext
  478.             final EditText et1 = (EditText) v.findViewById(R.id.editText1);
  479.             et1.setId(i + 400);
  480.            
  481.             //click listener for Other
  482.             cb4.setOnCheckedChangeListener(new OnCheckedChangeListener() {             
  483.                 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  484.                     //make visible specify other edittext
  485.                     CheckBox cb = (CheckBox) buttonView;
  486.                     if (isChecked)
  487.                         et1.setVisibility(View.VISIBLE);
  488.                     else {
  489.                         et1.setVisibility(View.GONE);
  490.                         et1.setText("");
  491.                     }      
  492.                 }
  493.             });        
  494.            
  495.             //add inflated view to linear layout
  496.             v.setVisibility(View.GONE);
  497.             insertPoint.addView(v);
  498.            
  499.             //disable views
  500.             cb1.setEnabled(false);
  501.             cb2.setEnabled(false);
  502.             cb3.setEnabled(false);
  503.             cb4.setEnabled(false);
  504.         }      
  505.     }
  506.    
  507.     //------------------------------------------------------------------------------------------------------
  508.     //open next activity when next step button clicked
  509.     //------------------------------------------------------------------------------------------------------
  510.     public void nextStep(View v) { 
  511.        
  512.         if (dataComplete()) {
  513.             //save entered data
  514.             saveEnteredData();     
  515.                        
  516.             //launch next activity
  517.             Intent questionIntent = new Intent(Question2Page2Activity.this, Question3Page1Activity.class);
  518.             questionIntent.putExtra("qDataSet", qDataSet);
  519.             startActivityForResult(questionIntent, nextRequestCode);
  520.         }
  521.         else
  522.             Toast.makeText(Question2Page2Activity.this, "First complete all questions on this page.", Toast.LENGTH_SHORT).show();
  523.        
  524.     }  
  525.    
  526.     //------------------------------------------------------------------------------------------------------
  527.     //check that all data is filled in
  528.     //------------------------------------------------------------------------------------------------------
  529.     private boolean dataComplete() {
  530.        
  531.         RelativeLayout rl = (RelativeLayout) findViewById(R.id.mainRelativeLayout);
  532.         boolean valid = true;
  533.        
  534.         for (int i = 0; i < rl.getChildCount(); i++) {
  535.             View v1 = rl.getChildAt(i);
  536.             Class c1 = v1.getClass();
  537.            
  538.             if (c1 == LinearLayout.class) {
  539.                 LinearLayout ll = (LinearLayout) v1;
  540.                 for (int j = 0; j < ll.getChildCount(); j++) {                 
  541.                     LinearLayout llInner = (LinearLayout) ll.getChildAt(j);
  542.                    
  543.                     for (int k = 0; k < llInner.getChildCount(); k++) {
  544.                         View v2 = llInner.getChildAt(k);
  545.                         Class c2 = v2.getClass();
  546.                        
  547.                         //check if spinner is visible and item selected
  548.                         if (c2 == NoDefaultSpinner.class) {
  549.                             NoDefaultSpinner s1 = (NoDefaultSpinner) v2;
  550.                            
  551.                             if (llInner.getVisibility() == View.VISIBLE && s1.getSelectedItem() == null)
  552.                                 valid = false;
  553.                         }
  554.                         //check if edittext from other is filled in                    
  555.                         else if (c2 == EditText.class) {
  556.                             EditText et1 = (EditText) v2;
  557.                            
  558.                             if (et1.getVisibility() == View.VISIBLE && et1.length() == 0)
  559.                                 valid = false;
  560.                         }
  561.                     }
  562.                    
  563.                 }
  564.             }
  565.         }
  566.         return valid;
  567.        
  568.     }
  569.    
  570.     //------------------------------------------------------------------------------------------------------
  571.     //save entered data to object of persondataset class
  572.     //------------------------------------------------------------------------------------------------------
  573.     private void saveEnteredData() {
  574.        
  575.         for (int i = 0; i < qDataSet.getPeopleNum(); i++) {
  576.            
  577.             //question 2.7
  578.             LinearLayout ll2_7 = (LinearLayout) findViewById(R.id.ll2_7);
  579.             NoDefaultSpinner s2_7 = (NoDefaultSpinner) ll2_7.getChildAt(i).findViewById(i);
  580.             String selected2_7 = s2_7.getSelectedItem() != null ? s2_7.getSelectedItem().toString() : "";
  581.             qDataSet.getPerson(i).setQ2_7(selected2_7);
  582.            
  583.             //question 2.8
  584.             LinearLayout ll2_8 = (LinearLayout) findViewById(R.id.ll2_8);
  585.             CheckBox cb1 = (CheckBox) ll2_8.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr1).findViewById(i);
  586.             CheckBox cb2 = (CheckBox) ll2_8.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr1).findViewById(i + 100);
  587.             CheckBox cb3 = (CheckBox) ll2_8.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr2).findViewById(i + 200);
  588.             CheckBox cb4 = (CheckBox) ll2_8.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr2).findViewById(i + 300);
  589.             CheckBox cb5 = (CheckBox) ll2_8.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr3).findViewById(i + 400);
  590.             CheckBox cb6 = (CheckBox) ll2_8.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr3).findViewById(i + 500);
  591.             CheckBox cb7 = (CheckBox) ll2_8.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr4).findViewById(i + 600);
  592.             CheckBox cb8 = (CheckBox) ll2_8.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr4).findViewById(i + 700);         
  593.             EditText et2_8 = (EditText) ll2_8.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr5).findViewById(i + 800);
  594.             //add disability
  595.             if (selected2_7.equals("Yes")) {
  596.                 //add other disability
  597.                 qDataSet.getPerson(i).setQ2_8_1(cb1.isChecked());
  598.                 qDataSet.getPerson(i).setQ2_8_2(cb2.isChecked());
  599.                 qDataSet.getPerson(i).setQ2_8_3(cb3.isChecked());
  600.                 qDataSet.getPerson(i).setQ2_8_4(cb4.isChecked());
  601.                 qDataSet.getPerson(i).setQ2_8_5(cb5.isChecked());
  602.                 qDataSet.getPerson(i).setQ2_8_6(cb6.isChecked());
  603.                 qDataSet.getPerson(i).setQ2_8_7(cb7.isChecked());
  604.                 qDataSet.getPerson(i).setQ2_8_8(et2_8.getText().toString());
  605.             }
  606.             //no disability
  607.             else {
  608.                 qDataSet.getPerson(i).setQ2_8_1(false);
  609.                 qDataSet.getPerson(i).setQ2_8_2(false);
  610.                 qDataSet.getPerson(i).setQ2_8_3(false);
  611.                 qDataSet.getPerson(i).setQ2_8_4(false);
  612.                 qDataSet.getPerson(i).setQ2_8_5(false);
  613.                 qDataSet.getPerson(i).setQ2_8_6(false);
  614.                 qDataSet.getPerson(i).setQ2_8_7(false);
  615.                 qDataSet.getPerson(i).setQ2_8_8("");
  616.             }
  617.            
  618.             //question 2.9
  619.             LinearLayout ll2_9 = (LinearLayout) findViewById(R.id.ll2_9);
  620.             NoDefaultSpinner s2_9 = (NoDefaultSpinner) ll2_9.getChildAt(i).findViewById(i);
  621.             String selected2_9 = s2_9.getSelectedItem() != null ? s2_9.getSelectedItem().toString() : "";
  622.             EditText et2_9 = (EditText) ll2_9.getChildAt(i).findViewById(i + 100);
  623.             //add other
  624.             if (s2_9.getSelectedItemPosition() == s2_9.getAdapter().getCount()-1 || s2_9.getSelectedItemPosition() == s2_9.getAdapter().getCount()-2)
  625.                 qDataSet.getPerson(i).setQ2_9(et2_9.getText().toString());             
  626.             //add preset
  627.             else
  628.                 qDataSet.getPerson(i).setQ2_9(selected2_9);
  629.            
  630.             //question 2.10
  631.             LinearLayout ll2_10 = (LinearLayout) findViewById(R.id.ll2_10);
  632.             NoDefaultSpinner s2_10 = (NoDefaultSpinner) ll2_10.getChildAt(i).findViewById(i);
  633.             String selected2_10 = s2_10.getSelectedItem() != null ? s2_10.getSelectedItem().toString() : "";
  634.             EditText et2_10 = (EditText) ll2_10.getChildAt(i).findViewById(i + 100);
  635.             //add other
  636.             if (s2_10.getSelectedItemPosition() == s2_10.getAdapter().getCount()-1)
  637.                 qDataSet.getPerson(i).setQ2_10(et2_10.getText().toString());               
  638.             //add preset
  639.             else
  640.                 qDataSet.getPerson(i).setQ2_10(selected2_10);  
  641.            
  642.             //question 2.11
  643.             LinearLayout ll2_11 = (LinearLayout) findViewById(R.id.ll2_11);
  644.             NoDefaultSpinner s2_11 = (NoDefaultSpinner) ll2_11.getChildAt(i).findViewById(i);
  645.             String selected2_11 = s2_11.getSelectedItem() != null ? s2_11.getSelectedItem().toString() : "";
  646.             if (ll2_11.getChildAt(i).getVisibility() != View.GONE)
  647.                 qDataSet.getPerson(i).setQ2_11(selected2_11);
  648.             else
  649.                 qDataSet.getPerson(i).setQ2_11("N/A");
  650.            
  651.             //question 2.11_1
  652.             LinearLayout ll2_11_1 = (LinearLayout) findViewById(R.id.ll2_11_1);
  653.             cb1 = (CheckBox) ll2_11_1.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr1).findViewById(i);
  654.             cb2 = (CheckBox) ll2_11_1.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr1).findViewById(i + 100);
  655.             cb3 = (CheckBox) ll2_11_1.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr2).findViewById(i + 200);
  656.             cb4 = (CheckBox) ll2_11_1.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr2).findViewById(i + 300);   
  657.             EditText et2_11_1 = (EditText) ll2_11_1.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr3).findViewById(i + 400);
  658.             //add license
  659.             if (selected2_11.equals("Yes")) {
  660.                 //add other license
  661.                 qDataSet.getPerson(i).setQ2_11_1_1(cb1.isChecked());
  662.                 qDataSet.getPerson(i).setQ2_11_1_2(cb2.isChecked());
  663.                 qDataSet.getPerson(i).setQ2_11_1_3(cb3.isChecked());
  664.                 qDataSet.getPerson(i).setQ2_11_1_4(et2_11_1.getText().toString());
  665.             }
  666.             //no license
  667.             else {
  668.                 qDataSet.getPerson(i).setQ2_11_1_1(false);
  669.                 qDataSet.getPerson(i).setQ2_11_1_2(false);
  670.                 qDataSet.getPerson(i).setQ2_11_1_3(false);
  671.                 qDataSet.getPerson(i).setQ2_11_1_4("");
  672.             }              
  673.            
  674.         }      
  675.     }  
  676.    
  677.     //------------------------------------------------------------------------------------------------------
  678.     //button pressed to go to previuos section. Return qdataset as serialized.
  679.     //------------------------------------------------------------------------------------------------------   
  680.     public void previousStep(View v) {
  681.        
  682.         //save entered data
  683.         saveEnteredData();
  684.        
  685.         Intent questionIntent = new Intent();
  686.         questionIntent.putExtra("qDataSet", qDataSet);
  687.         setResult(RESULT_OK, questionIntent);
  688.         finish();  
  689.     }
  690.    
  691.     //------------------------------------------------------------------------------------------------------
  692.     //redirects back pressed event to previousStep method
  693.     //------------------------------------------------------------------------------------------------------       
  694.     public void onBackPressed() {
  695.        
  696.         previousStep(null);
  697.     }  
  698.    
  699.     //------------------------------------------------------------------------------------------------------
  700.     //save returned qDataset
  701.     //------------------------------------------------------------------------------------------------------
  702.     public void onActivityResult(int requestCode, int resultCode, Intent data) {
  703.        
  704.         if (resultCode == RESULT_OK && requestCode == nextRequestCode) {
  705.            
  706.             //retrieve serialized qdataset                 
  707.             qDataSet = (QuestionnaireDataSet) data.getSerializableExtra("qDataSet");           
  708.         }
  709.     }  
  710.    
  711.     //------------------------------------------------------------------------------------------------------
  712.     //save returned qDataset
  713.     //------------------------------------------------------------------------------------------------------   
  714.     private void populateFields() {
  715.        
  716.         LinearLayout ll2_7 = (LinearLayout) findViewById(R.id.ll2_7);
  717.         LinearLayout ll2_8 = (LinearLayout) findViewById(R.id.ll2_8);
  718.         LinearLayout ll2_9 = (LinearLayout) findViewById(R.id.ll2_9);
  719.         LinearLayout ll2_10 = (LinearLayout) findViewById(R.id.ll2_10);
  720.         LinearLayout ll2_11 = (LinearLayout) findViewById(R.id.ll2_11);
  721.         LinearLayout ll2_11_1 = (LinearLayout) findViewById(R.id.ll2_11_1);
  722.        
  723.         for (int i = 0; i < qDataSet.getPeopleNum(); i++) {
  724.            
  725.             PersonDataSet person = qDataSet.getPerson(i);
  726.            
  727.             //question 2.7     
  728.             if (person.getQ2_7() != null) {
  729.                
  730.                 NoDefaultSpinner s2_7 = (NoDefaultSpinner) ll2_7.getChildAt(i).findViewById(i);
  731.                 ArrayAdapter<CharSequence> arrAdapter = ArrayAdapter.createFromResource(this, R.array.question2_7_spinner, android.R.layout.simple_spinner_item);              
  732.                 int position = arrAdapter.getPosition(person.getQ2_7());
  733.                
  734.                 s2_7.setSelection(position);           
  735.             }          
  736.            
  737.             //question 2.8 
  738.             if (person.getQ2_8_1() != null) {  
  739.                 CheckBox cb1 = (CheckBox) ll2_8.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr1).findViewById(i);
  740.                 CheckBox cb2 = (CheckBox) ll2_8.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr1).findViewById(i + 100);
  741.                 CheckBox cb3 = (CheckBox) ll2_8.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr2).findViewById(i + 200);
  742.                 CheckBox cb4 = (CheckBox) ll2_8.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr2).findViewById(i + 300);
  743.                 CheckBox cb5 = (CheckBox) ll2_8.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr3).findViewById(i + 400);
  744.                 CheckBox cb6 = (CheckBox) ll2_8.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr3).findViewById(i + 500);
  745.                 CheckBox cb7 = (CheckBox) ll2_8.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr4).findViewById(i + 600);
  746.                 CheckBox cb8 = (CheckBox) ll2_8.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr4).findViewById(i + 700);         
  747.                 EditText et2_8 = (EditText) ll2_8.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr5).findViewById(i + 800);               
  748.                
  749.                 cb1.setChecked(person.getQ2_8_1());
  750.                 cb2.setChecked(person.getQ2_8_2());
  751.                 cb3.setChecked(person.getQ2_8_3());
  752.                 cb4.setChecked(person.getQ2_8_4());
  753.                 cb5.setChecked(person.getQ2_8_5());
  754.                 cb6.setChecked(person.getQ2_8_6());
  755.                 cb7.setChecked(person.getQ2_8_7());
  756.                 cb8.setChecked(person.getQ2_8_8().length() > 0 ? true : false);
  757.                 et2_8.setText(person.getQ2_8_8());
  758.             }
  759.            
  760.             //question 2.9
  761.             if (person.getQ2_9() != null) {
  762.                
  763.                 NoDefaultSpinner s2_9 = (NoDefaultSpinner) ll2_9.getChildAt(i).findViewById(i);
  764.                 EditText et2_9 = (EditText) ll2_9.getChildAt(i).findViewById(i + 100);
  765.                 ArrayAdapter<CharSequence> arrAdapter = ArrayAdapter.createFromResource(this, R.array.question2_9_spinner, android.R.layout.simple_spinner_item);
  766.                 int position = arrAdapter.getPosition(person.getQ2_9());
  767.                
  768.                 if (position != -1)
  769.                     s2_9.setSelection(position);
  770.                 else if (!person.getQ2_9().equals("N/A") && !person.getQ2_9().equals("")) {
  771.                     s2_9.setSelection(s2_9.getAdapter().getCount()-1);
  772.                     et2_9.setText(person.getQ2_9());
  773.                 }
  774.             }          
  775.            
  776.             //question 2.10
  777.             if (person.getQ2_10() != null) {   
  778.                
  779.                 NoDefaultSpinner s2_10 = (NoDefaultSpinner) ll2_10.getChildAt(i).findViewById(i);
  780.                 EditText et2_10 = (EditText) ll2_10.getChildAt(i).findViewById(i + 100);
  781.                 ArrayAdapter<CharSequence> arrAdapter = ArrayAdapter.createFromResource(this, R.array.question2_10_spinner, android.R.layout.simple_spinner_item);
  782.                 int position = arrAdapter.getPosition(person.getQ2_10());
  783.                
  784.                 if (position != -1)
  785.                     s2_10.setSelection(position);
  786.                 else if (!person.getQ2_10().equals("N/A") && !person.getQ2_10().equals("")) {
  787.                     s2_10.setSelection(s2_10.getAdapter().getCount()-1);
  788.                     et2_10.setText(person.getQ2_10());
  789.                 }
  790.             }                          
  791.            
  792.             //question 2.11
  793.             if (person.getQ2_11() != null) {   
  794.                
  795.                 NoDefaultSpinner s2_11 = (NoDefaultSpinner) ll2_11.getChildAt(i).findViewById(i);
  796.                 ArrayAdapter<CharSequence> arrAdapter = ArrayAdapter.createFromResource(this, R.array.question2_11_spinner, android.R.layout.simple_spinner_item);
  797.                 int position = arrAdapter.getPosition(person.getQ2_11());
  798.                
  799.                 if (position != -1)
  800.                     s2_11.setSelection(position);
  801.                 else
  802.                     s2_11.setSelection(-1);    
  803.             }
  804.            
  805.             //question 2.11_1
  806.             if (person.getQ2_11_1_1() != null) {   
  807.                
  808.                 CheckBox cb1 = (CheckBox) ll2_11_1.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr1).findViewById(i);
  809.                 CheckBox cb2 = (CheckBox) ll2_11_1.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr1).findViewById(i + 100);
  810.                 CheckBox cb3 = (CheckBox) ll2_11_1.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr2).findViewById(i + 200);
  811.                 CheckBox cb4 = (CheckBox) ll2_11_1.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr2).findViewById(i + 300);
  812.                 EditText et2_11_1 = (EditText) ll2_11_1.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr3).findViewById(i + 400); 
  813.                
  814.                 cb1.setChecked(qDataSet.getPerson(i).getQ2_11_1_1());
  815.                 cb2.setChecked(qDataSet.getPerson(i).getQ2_11_1_2());
  816.                 cb3.setChecked(qDataSet.getPerson(i).getQ2_11_1_3());
  817.                 cb4.setChecked(qDataSet.getPerson(i).getQ2_11_1_4().length() > 0 ? true : false);
  818.                 et2_11_1.setText(qDataSet.getPerson(i).getQ2_11_1_4());
  819.             }          
  820.         }      
  821.     }      
  822.    
  823. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement