0

我有一个数字调查的 Android 应用程序,我的应用程序从服务器(mysql-php)获取数据并显示在 Activity 中。我的疑问是如何将数据保存到数据库,因为我动态生成了问题和答案选项。

这是活动类的代码

       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));
        }



   }

对不起,我的英语不好

4

1 回答 1

0

您可以重新捕获从用户那里收集的答案,并将它们发送回服务器以使用您在此处使用的 Http 客户端请求和名称值对进行数据库处理。

于 2012-12-08T00:54:13.867 回答