dougaodi8895 2012-12-07 23:34
浏览 66

从asynctask动态生成的EditText,Checkbox,Radiobutton中保存数据库中的数据?

i have a Android App of Digital Survey, my app get data from the server(mysql-php) and display in Activity . My doubt is how save de data to the Database because i generated dynamically the questions and answer options .

Here is the Code of the Activity Class

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



   }

Sorry for my bad English

  • 写回答

1条回答

  • douba5540 2012-12-08 00:54
    关注

    You can re capture the answers collected from the user and send them back to the server for database processing with the Http client request and namevaluepairs as you used here.

    评论

报告相同问题?

悬赏问题

  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备