2012-12-07 50 views
0

我有一個數字調查Android應用程序,我的應用程序從服務器(mysql-php)獲取數據並顯示在Activity中。我的疑問是如何將數據保存到數據庫,因爲我動態生成了問題和答案選項。保存數據從的AsyncTask動態生成的EditText,複選框,單選框?

下面是活動類的代碼

 public void onPostExecute(String result) { 

     pDialog.dismiss();//ocultamos progess dialog. 


     //paring data 
     String id_preg; 
     String preg; 
     String tipopreg; 
     String tipo_opc; 
     String descri_op; 
     String id_pregunta = null; 
     String pregunta = null; 
     String tipo_pregunta = null; 
     String tipo_opcion = null; 
     String descrip_opc; 

     try{ 
      JSONArray jArray = new JSONArray(result); 
     JSONObject json_data=null; 
    String[] tipopregu=new String[jArray.length()]; 
     String[] tipopcion=new String[jArray.length()]; 
     String[] id_preguntas=new String[jArray.length()]; 
     String[] descripcion_opc=new String[jArray.length()];  
    String aux1=""; 
    String aux2=""; 
    String aux3=""; 
    String aux4=""; 
    String auxtipoopc; 
    int cont=1; 
    int cont2=0; 
    //validate the values not repeteated because 
     for(int i=0;i<jArray.length();i++){ 


       json_data = jArray.getJSONObject(i); 
       //Get DAta 
       id_preg =json_data.getString("ID_PREGUNTA");//IS QUESTION ID 
       preg = json_data.getString("DESCRIPCION");//DESCRIPTION 
       tipopreg=json_data.getString("TIPO_PREGUNTA"); //TYPE OF        QUESTION 
       tipo_opc=json_data.getString("ID_TIPOOPC");//VALUE OF ID TYPE OPTION 
       auxtipoopc=tipo_opc; 
       descrip_opc=json_data.getString("DESCRIP_OPC");//DESCRIPTION OF VALUES USED IN MULTIPLES VALUES 
     if((!aux1.equals(id_preg))||(!aux2.equals(preg))||(!aux3.equals(tipopreg))||(!aux4.equals(tipo_opc))){ 
       id_pregunta=id_preg; 
       pregunta=preg; 
       tipo_pregunta=tipopreg; 
       tipo_opcion=tipo_opc; 
       nameValuePairs.add(new list<String, String>(id_pregunta, pregunta)); 
       id_preguntas[cont2]=id_pregunta; 
       tipopregu[cont2]=tipo_pregunta; 
       tipopcion[cont2]=tipo_opcion; 
       Log.e("id_pregunta", id_pregunta); 

       Log.e("pregunta",pregunta); 
        Log.e("id_preg-pregunta", id_pregunta+" "+pregunta); 
        Log.e("tipo_pregunta",tipo_pregunta); 



       Log.e("tipo_opc",tipo_opcion); 

       cont2=cont2+1; 
       Log.e("contador2",String.valueOf(cont2)); 
     } 


       descripcion_opc[i]=descrip_opc; 
      nameValuePairs2.add(new list<String, String>(auxtipoopc, descrip_opc)); 
      Log.e("Id_tipo_opc--Opcion", auxtipoopc+" "+descrip_opc); 
      aux1=id_preg; 
       aux1=preg; 
       aux3=tipopreg; 
       aux4=tipo_opc; 

      cont=cont+1; 
      Log.e("contador1",String.valueOf(cont)); 
     } 
    List<NameValuePair> datos = new ArrayList<NameValuePair>(); 
     datos=nameValuePairs; 



    List<EditText> allEds = new ArrayList<EditText>(); 

    llinear=(LinearLayout) findViewById(R.id.preg); 

    final int N=cont2+1; 
    final TextView[] tx = new TextView[N]; 
      int j=0;   
    for (NameValuePair i:nameValuePairs) { 


     String id_preguntastr=i.getName(); 
     String preguntastr=i.getValue(); 
     String tipopregun=tipopregu[j]; 
     String id_opcion=tipopcion[j]; 
     String id_preguntaaux=id_preguntas[j]; 

     final TextView rowTextView = new TextView(getBaseContext()); 
     rowTextView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
     rowTextView.setText(id_preguntastr+" "+preguntastr); 



     llinear.addView(rowTextView); 
     tx[j] = rowTextView; 



     if(tipopregun.equals("SI-NO"))// IF QUESTION IS YES/NO 
     { 
      RadioGroup radiogroup1 = new RadioGroup(getBaseContext()); 
      final RadioButton rowradiobuttsi = new RadioButton(getBaseContext()); 
      rowradiobuttsi.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
      rowradiobuttsi.setText("SI"); 
      radiogroup1.addView(rowradiobuttsi); 
      final RadioButton rowradiobuttno = new RadioButton(getBaseContext()); 
      rowradiobuttno.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
      rowradiobuttno.setText("NO"); 

       radiogroup1.addView(rowradiobuttno); 
      llinear.addView(radiogroup1) ; 

     } 
     if(tipopregun.equals("Verdadero-Falso"))//IF QUESTION IS TYPE TRUE/FALSE  
     { 
      RadioGroup radiogroup2 = new RadioGroup(getBaseContext()); 
       final RadioButton rowradiobuttv = new RadioButton(getBaseContext()); 
       rowradiobuttv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
       rowradiobuttv.setText("VERDADERO"); 
       radiogroup2.addView(rowradiobuttv); 
       final RadioButton rowradiobuttf = new RadioButton(getBaseContext()); 
       rowradiobuttf.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
       rowradiobuttf.setText("FALSO"); 

        radiogroup2.addView(rowradiobuttf); 
       llinear.addView(radiogroup2) ; 
     } 

     if(tipopregun.equals("Valor")) //IF QUESTION IS A VALUE(EDITTEXT) 
     { 




      //count is number of edittext fields 
      final EditText ed = new EditText(getBaseContext()); 
     ed.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
     allEds.add(ed); 

     llinear.addView(ed); 

     } 

     if(tipopregun.equals("opcionmultiple"))//MULTIPLE OPTIONS (CHECKBOXs) 
     { 

      for (NameValuePair k:nameValuePairs2) { 
       String tipoopcion=k.getName(); 
       String opcion=k.getValue(); 

       if(tipoopcion.equalsIgnoreCase("3")){ 


       final CheckBox checkboxoptionsm = new CheckBox(getBaseContext()); 
       checkboxoptionsm.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
       checkboxoptionsm.setText(opcion); 
       allChbx.add(checkboxoptionsm); 
       llinear.addView(checkboxoptionsm); 
       } 
      } 

       } 

     j=j+1; 
    } 



     }catch(JSONException e4){ 
      Toast.makeText(getBaseContext(), "Preguntas no Encontrada"+e4, Toast.LENGTH_LONG).show(); 
      Log.e("fallo", "fallo", e4); 
     // Log.e("pairs",nameValuePairs); 
     } 
     catch(Exception e9){ 
      Log.e("error", String.valueOf(e9)); 
     } 



    } 

對不起,我英文不好

+1

您能夠反序列化問題以及顯示它們的選項。所以你可以用相反的方式序列化它們。 – yDelouis

回答

0

您可以重新捕獲從用戶收集的答案,並與它們發送回服務器的數據庫處理你在這裏使用的Http客戶端請求和namevaluepairs。