ProbabilityAll.java :  » UnTagged » smartresearch » com » andromeda » smartresearch » Android Open Source

Android Open Source » UnTagged » smartresearch 
smartresearch » com » andromeda » smartresearch » ProbabilityAll.java
package com.andromeda.smartresearch;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.andromeda.smartresearch.vo.QuestionVO;
import com.andromeda.smartresearch.vo.ResultVO;

public class ProbabilityAll extends ListActivity implements OnClickListener{
  
  TextView tv_all_participated_person, tv_all_question_subjectNo, tv_all_question_subject;
  
  //
  TextView tv_all_rb[]=new TextView[5];
  int all_rb_id[]={R.id.tv_probability_rb1, R.id.tv_probability_rb2, R.id.tv_probability_rb3, R.id.tv_probability_rb4, R.id.tv_probability_rb5}; 
  final int all_rb_id_size=all_rb_id.length;
  
  TextView tv_all_rb_percent[]=new TextView[5];
  int all_rb_percent_id[]={R.id.tv_probability_rb1_percent, R.id.tv_probability_rb2_percent, R.id.tv_probability_rb3_percent, R.id.tv_probability_rb4_percent, R.id.tv_probability_rb5_percent};
  final int all_rb_percent_id_size=all_rb_percent_id.length;
  
  ProgressBar pb_all_rb_field[]=new ProgressBar[5];
  int all_rb_field_id[]={R.id.pd_all_rb_field1, R.id.pd_all_rb_field2, R.id.pd_all_rb_field3, R.id.pd_all_rb_field4, R.id.pd_all_rb_field5};
  final int all_rb_field_id_size=all_rb_field_id.length;
  
  //
  TextView tv_all_cb[]=new TextView[5];
  int all_cb_id[]={R.id.tv_probability_cb1, R.id.tv_probability_cb2, R.id.tv_probability_cb3, R.id.tv_probability_cb4, R.id.tv_probability_cb5}; 
  final int all_cb_id_size=all_cb_id.length;
  
  TextView tv_all_cb_percent[]=new TextView[5];
  int all_cb_percent_id[]={R.id.tv_probability_cb1_percent, R.id.tv_probability_cb2_percent, R.id.tv_probability_cb3_percent, R.id.tv_probability_cb4_percent, R.id.tv_probability_cb5_percent};
  final int all_cb_percent_id_size=all_cb_percent_id.length;
  
  ProgressBar pb_all_cb_field[]=new ProgressBar[5];
  int all_cb_field_id[]={R.id.pd_all_cb_field1, R.id.pd_all_cb_field2, R.id.pd_all_cb_field3, R.id.pd_all_cb_field4, R.id.pd_all_cb_field5};
  final int all_cb_field_id_size=all_cb_field_id.length;
  
  //
  LinearLayout ll_all_radio, ll_all_edit, ll_all_check;
  
  //
  Button prevBtn, okBtn, nextBtn;
  
  ArrayList<QuestionVO> questionList;
  ArrayList<ResultVO> resultList;
  int researchNo;
  int presentQuestionType;
  int presentQuestionNO;
  int valueCount=0;
  ProgressDialog pd;
  int participatedPerson=0;
  int currentPosition=0;
    
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.probability_all);
    
    tv_all_participated_person=(TextView) findViewById(R.id.tv_all_probability_participated_person);
    tv_all_question_subjectNo=(TextView) findViewById(R.id.tv_all_probability_question_subjectNo);
    tv_all_question_subject=(TextView) findViewById(R.id.tv_all_probability_question_subject);
    
    //  
    ll_all_radio=(LinearLayout) findViewById(R.id.ll_all_radio);
    ll_all_edit=(LinearLayout) findViewById(R.id.ll_all_edit);
    ll_all_check=(LinearLayout) findViewById(R.id.ll_all_check);
    
    
    //
    for(int i=0; i<all_rb_id_size; i++){ // - 
      tv_all_rb[i]=(TextView) findViewById(all_rb_id[i]);
    }
    
    for(int i=0; i<all_rb_field_id_size; i++){ // - 
      pb_all_rb_field[i]=(ProgressBar) findViewById(all_rb_field_id[i]);
    }    
    
    for(int i=0; i<all_rb_percent_id_size; i++){ // -  
      tv_all_rb_percent[i]=(TextView) findViewById(all_rb_percent_id[i]);
    }
    
    //
    for(int i=0; i<all_cb_id_size; i++){ // - 
      tv_all_cb[i]=(TextView) findViewById(all_cb_id[i]);
    }
    
    for(int i=0; i<all_cb_field_id_size; i++){ // - 
      pb_all_cb_field[i]=(ProgressBar) findViewById(all_cb_field_id[i]);
    }    
    
    for(int i=0; i<all_cb_percent_id_size; i++){ // -  
      tv_all_cb_percent[i]=(TextView) findViewById(all_cb_percent_id[i]);
    }
    
    prevBtn=(Button) findViewById(R.id.bt_probability_prevBtn);
    okBtn=(Button) findViewById(R.id.bt_probability_okBtn);
    nextBtn=(Button) findViewById(R.id.bt_probability_nextBtn);

    prevBtn.setOnClickListener(this);
    okBtn.setOnClickListener(this);
    nextBtn.setOnClickListener(this);
    
    Intent intent=getIntent();
    questionList=(ArrayList<QuestionVO>) intent.getSerializableExtra("questionList");
    researchNo=intent.getIntExtra("researchNO", 0);
    pd=ProgressDialog.show(ProbabilityAll.this, "", "  . \n .");
    connectServer();    
  }
  
  //   
  private void viewProbability(QuestionVO questionVO) {
    presentQuestionType=Integer.parseInt(questionVO.getQuestionType());
    String subject=null;
    String []slicedValue;
    
    tv_all_participated_person.setText(" : " + participatedPerson + "");
    
    presentQuestionNO=Integer.parseInt(questionVO.getQuestionNO());
    tv_all_question_subjectNo.setText("Question"+presentQuestionNO+"."); // 
    
    subject=questionVO.getQuestionSubject();
    tv_all_question_subject.setText("  "+subject); //
    
    slicedValue=getSlicedValue(questionVO.getQuestionValue());
    valueCount=slicedValue.length; //
    ArrayList<String> inputvalueList = new ArrayList<String>();
    inputvalueList.clear();
    Log.i("currentPosition",""+currentPosition);
    int inputValueSize = resultList.get(currentPosition).getQuestionInputValue().split("`").length;
    Log.i("QuestionInputValue",""+resultList.get(currentPosition).getQuestionInputValue());
    for(int i=0; i<inputValueSize; i++){
      inputvalueList.add(resultList.get(currentPosition).getQuestionInputValue().split("`")[i]);
      Log.i("inputvalue",inputvalueList.get(i));
    }
    
    switch(presentQuestionType){
    case 0: //
      ll_all_radio.setVisibility(View.VISIBLE);
      ll_all_edit.setVisibility(View.GONE);
      ll_all_check.setVisibility(View.GONE);

      for(int i=4; i>=valueCount; i--){
        tv_all_rb[i].setVisibility(View.INVISIBLE); //
        pb_all_rb_field[i].setVisibility(View.INVISIBLE); //
        tv_all_rb_percent[i].setVisibility(View.INVISIBLE); //
      }

      for(int i=0; i<valueCount; i++){ //
        tv_all_rb[i].setText((i+1) + ". " + slicedValue[i]);
        int getValue=Integer.parseInt(inputvalueList.get(i));
        int setValue=(int)((double)getValue/participatedPerson*100);
        pb_all_rb_field[i].setProgress(setValue);
        tv_all_rb_percent[i].setText(" "+String.valueOf(setValue)+"%("+getValue+"/"+participatedPerson+")");
      }
      break;
      
    case 1: //
      ll_all_radio.setVisibility(View.GONE);
      ll_all_edit.setVisibility(View.VISIBLE);
      ll_all_check.setVisibility(View.GONE);    
      break;
      
    case 2: //
      ll_all_radio.setVisibility(View.GONE);
      ll_all_edit.setVisibility(View.GONE);
      ll_all_check.setVisibility(View.VISIBLE);

      for(int i=4; i>=valueCount; i--){
        tv_all_cb[i].setVisibility(View.INVISIBLE); //
        pb_all_cb_field[i].setVisibility(View.INVISIBLE); //
        tv_all_cb_percent[i].setVisibility(View.INVISIBLE); //
      }

      for(int i=0; i<valueCount; i++){ //
        tv_all_cb[i].setText((i+1) + ". " + slicedValue[i]);
        int getValue=Integer.parseInt(inputvalueList.get(i));
        int totalValue = Integer.parseInt(resultList.get(currentPosition).getParticipatedPerson());
        int setValue=(int)(((double)getValue/totalValue)*100);
        

        Log.i("totalValue", ""+totalValue);
        pb_all_cb_field[i].setProgress(setValue);
        tv_all_cb_percent[i].setText(" "+String.valueOf(setValue)+"%("+getValue+"/"+totalValue+")");
      }      
      break;
    }
    
    if(presentQuestionNO==questionList.size()){ //-X
      nextBtn.setEnabled(false);
    }else if(presentQuestionNO==1){ //-X
      prevBtn.setEnabled(false);
    }else{ 
      prevBtn.setEnabled(true);  
      nextBtn.setEnabled(true);
    }
  }
  

  private String[] getSlicedValue(String questionValue) { //  (split)
    return questionValue.split("`");
  }
  
  
  @Override
  public void onClick(View v) {

    switch (v.getId()) {
    case R.id.bt_probability_prevBtn:
      currentPosition--;
      viewProbability(questionList.get(currentPosition));  
      break;

    case R.id.bt_probability_okBtn:
      finish();
      break;
      
    case R.id.bt_probability_nextBtn:
      currentPosition++;
      viewProbability(questionList.get(currentPosition));
      break;
    }
  }

  
  
  private final Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
      pd.dismiss();
      resultList=(ArrayList<ResultVO>) msg.getData().getSerializable("resultList");
      if(resultList.size()!=0){
        viewProbability(questionList.get(currentPosition));
      } else {
        Toast.makeText(ProbabilityAll.this, "    .", Toast.LENGTH_SHORT).show();
        finish();
      }
      
    }
  };

  private void connectServer() {
    final ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

      @Override
      public String handleResponse(HttpResponse response)
      throws ClientProtocolException, IOException {
        ArrayList<ResultVO> resultList=new ArrayList<ResultVO>();
        HttpEntity entity = response.getEntity();        
        resultList = parsingData(entity.getContent());
        Message message =handler.obtainMessage();
        Bundle bundle = new Bundle();
        bundle.putSerializable("resultList", resultList);
        message.setData(bundle);
        handler.sendMessage(message);
        return null;
      }
    };
    new Thread (new Runnable() {
      @Override
      public void run() {
        try {
          String url="http://172.16.1.37:8080/SmartDB/allStatsGet.do";
          HttpClient http = new DefaultHttpClient();
          HttpParams params = http.getParams();
          params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
          HttpConnectionParams.setConnectionTimeout(params, 5000);
          HttpConnectionParams.setSoTimeout(params, 5000);

          //   .
          ArrayList<NameValuePair> getResearchParam = new ArrayList<NameValuePair>();
          getResearchParam.add(new BasicNameValuePair("researchNO", ""+researchNo));

          HttpPost httpPost = new HttpPost(url);
          UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(getResearchParam,"UTF-8");
          httpPost.setEntity(encodedFormEntity);
          http.execute(httpPost, responseHandler);

        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }).start();
  }

  protected ArrayList<ResultVO> parsingData(InputStream content) {
    ArrayList<ResultVO> parsedresultList = new ArrayList<ResultVO>();
    try {
      DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
      DocumentBuilder builder=factory.newDocumentBuilder();
      Document document=builder.parse(content);

      Element root= document.getDocumentElement(); //allstats

      NodeList viewes=root.getElementsByTagName("view"); 
      
      int item_count=viewes.getLength();
      
      for(int i=0; i<item_count; i++){ 
        Node view=viewes.item(i); //view
        Element element = (Element) view;
        participatedPerson = Integer.parseInt(element.getAttribute("member")); //view-member()
              
        NodeList questiones = element.getElementsByTagName("question");
        
        String questionNO=null;
        int totalMember=0;
        NodeList values = null;
        String value = null;
        final int questionCount = questiones.getLength();
        for(int j=0; j<questionCount; j++){
          Node question=questiones.item(j); //question
          Log.i("nodeName", question.getNodeName());
          element = (Element) question;
          questionNO=element.getAttribute("no"); //question-no
          Log.i("questionNO", ""+questionNO);
          totalMember=Integer.parseInt(element.getAttribute("totalnumber")); //question-totalnumber
          Log.i("totalMember", ""+totalMember);

          values=element.getElementsByTagName("values"); //values
          value=values.item(0).getTextContent(); //value

          ResultVO resultVO=new ResultVO("", value, ""+researchNo, questionNO, ""+totalMember);
          parsedresultList.add(resultVO);  
        }      
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return parsedresultList;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.