0

我需要帮助来检查我用来回答我的问题的所有 editText 是否为空。我把所有的答案都列在列表中。因此,当我单击问题末尾的提交按钮时,我可以发送所有答案。但我很困惑如何检查我拥有的所有编辑文本。
编辑文本将根据问题的总数出现。
示例:我有 5 个问题,我显示问题 1 页 1 问题。当我单击下一步按钮时,下一页将显示第二个问题,其中包含空白编辑文本。问题完成之前就是这样。我想要的是,当我单击发送/确定时,它将检查所有编辑文本我真的需要帮助,请帮助我.. 谢谢

String[] answerCollection;
String[] questionCollection;
private int questionPos = 0;
private EditText jawaban;

final Button kirim = (Button) findViewById(R.id.kirim);
                kirim.setOnClickListener(new OnClickListener() {
                    
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        if(v == kirim){
                            AlertDialog.Builder builder = new Builder(TugasSoal.this);
                            builder.setMessage("Apakah Anda yakin akan mengirim jawaban Anda? Anda tidak dapat mengakses kembali tugas yang sudah dikirim");
                            builder.setTitle("Confirmation Dialog");
    
                            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                                        // do something after confirm
                                    
                                    String finalAnswer = "";
                                    answerCollection[questionPos] = jawaban.getText().toString();
                                    
                                    for(int i = 0;i <answerCollection.length;i++){
                                        finalAnswer += (i+1) + "." + answerCollection[i] + "\n\n"; 
                                    }
                                    
                                    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                                    postParameters.add(new BasicNameValuePair("jawab", finalAnswer));
    
                                    
                                    String response = null;
    
                                    try {
                                        linkurl = new Koneksi(TugasSoal.this);
                                        SERVER_URL = linkurl.getUrl();
                                        SERVER_URL += "/mobile/tugasKirimTeks.php?idtgs="+param2+"&idu="+param3;
                                       response = CourseHttpClient.executeHttpPost(SERVER_URL, postParameters);
    
                                       String res = response.toString();
    
                                       res = res.trim();
    
                                       res = res.replaceAll("\\s+","");
                                       if(res.equals("1")){
                                           createDialog("Selamat", "Jawaban Anda Berhasil Dikirim");
                                           finish();
                                       }else
                                       {
                                           createDialog("Maaf", "Jawaban Anda Gagal Terkirim");
                                       }
                                    }
    
                                    catch (Exception e) {
    
                                       jawaban.setText(e.toString());
    
                                    }
                                }
                                
                            }); 
    
                            builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.cancel();
                                }
                            }); 
    
                            builder.create().show();    
                        }
                     }
                });

下一个和上一个部分

prev.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    answerCollection[questionPos] = jawaban.getText().toString();
                    
                    if(questionPos > 0){
                        questionPos -=1;
                        RefreshQuestionandAnswer();
//                      next.setEnabled(true);
//                      if(questionPos == 0)
//                          back.setEnabled(false);
                    }
                }
            });
            
            next.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    answerCollection[questionPos] = jawaban.getText().toString();
                    
                    if(questionPos < questionCollection.length - 1) {
                        questionPos +=1;
                        RefreshQuestionandAnswer();
//                      prev.setEnabled(true);
//                      if(questionPos == questionCollection.length - 1)
//                          next.setEnabled(false);
                    }
                }
            });

显示问题部分

linkurl = new Koneksi(this);
            SERVER_URL = linkurl.getUrl();
            SERVER_URL += "/mobile/TugasSoal.php?idc="+param1+"&ida="+param2+"&idu="+param3;

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(SERVER_URL);
            
            //parameter
            ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();{
            try {
               //add parameter
                httpPost.setEntity(new UrlEncodedFormEntity(param));
               
              HttpResponse httpRespose = httpClient.execute(httpPost);
              HttpEntity httpEntity = httpRespose.getEntity();
              
              //read content
              InputStream in = httpEntity.getContent();
              BufferedReader read = new BufferedReader(new InputStreamReader(in));
              
              String content = "";
              String line = "";

              while((line = read.readLine())!=null){
                 content += line;
              }
              
              Log.d("ADBUG", "content: "+content);
              
              
              //json
              if(!content.equals("null")){
              
                 try {
                    JSONArray jArr = new JSONArray(content);
                    String namaTugas="";
                    String detailTugas="";

                    JSONObject jsonObj = jArr.getJSONObject(0);
                    namaTugas = jsonObj.getString("name");
                    detailTugas = jsonObj.getString("description");
                    
                    questionCollection = detailTugas.split("\n");
                    answerCollection = new String[questionCollection.length];
                    judulTugas.setText(namaTugas);
                    
                    RefreshQuestionandAnswer();
                   
                 } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                 }
                 
              }else{
                 Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();
              }
              
           } catch (ClientProtocolException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
           } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
           }
       
            }
            
        }
        
        private void RefreshQuestionandAnswer()
        {
            jawaban.setText(answerCollection[questionPos]);
            isiTugas.setText(questionCollection[questionPos]);
        }

当我第一次打开菜单回答问题时 当我第一次打开菜单回答问题时

在此处输入图像描述

在我点击下一步按钮后

4

2 回答 2

1

这就是我要做的:

我会保留用户答案的​​ ArrayList (在索引 0 处,会有 answer1,索引 n 将是 answer (n-1))。

我还会保留一个“currentQuestionIndex”int 成员来指示我当前显示的问题(从 0 开始)

每次用户单击按钮时,我都会将该答案保存在 ArrayList 中。

answersArrayList.add(myEditText.getText().toString());

当需要提出新问题时,我不会开始新活动。

处理“返回”键:

您可以覆盖 onBackPressed() 方法,并使用 currentQuestionIndex 成员来决定是显示上一个问题还是退出活动。

但我觉得这很烦人(如果我在第 5 个问题,我需要按 5 次!)。我要做的是添加一个“下一个问题”和“上一个问题”按钮来浏览问题,当按下返回时,我会显示一个对话框,询问用户是否要退出

于 2013-07-03T06:49:14.203 回答
0

对于“显示上一个问题的答案”部分:
我会制作一个 ArrayList 类似于:

    List answerList = new ArrayList(); 


    next.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        answerCollection[questionPos] = jawaban.getText().toString();

                        if(questionPos < questionCollection.length - 1) {
                            if (answerCollection.get(questionPos).toString().equals("")){
                                answerList.add(questionPos, "0");//You check if the user has enetered an answer, if yes, it put a 1 into the array, if not, a 0
                            } else {
                                answerList.add(questionPos, "1");
                            }
                            questionPos +=1;
                            RefreshQuestionandAnswer();
    //                      prev.setEnabled(true);
    //                      if(questionPos == questionCollection.length - 1)
    //                      next.setEnabled(false);
                        }
                    }
                });

然后在发送按钮处,检查 Arraylist 是否为零。

for (int i = 0; i < answerList.size(); i++){
    if (answerList.get(i).toString().equals("0")){
        //no answer is given to question number i      
    }
}

像这样的东西

于 2013-07-03T08:20:36.703 回答