Pick up various values and play within a variable

Asked

Viewed 461 times

1

I needed to get all the answers inside Boolean respostas1 and play inside another variable so I can send it to the Mysql database.

The way I did, he’s returning me all false.

For example, I have 4 questions, he should bring me: true, false, true and false...

and is returning false false false.

Why it is returning false and how to solve?

for (int x=0; x<respostas2.length; x++){
for (final String a : ids){
    idPERGUNTAS = a;

}

Log.i("ID da Perguntas", "IDS das Perguntas: "+idPERGUNTAS);
//parametrosPost2.add(new BasicNameValuePair("ids", idPERGUNTAS));

for (String IDPaciente : idPacientes){
    for (int i=0; i < respostas2.length; i++){
        idPACIENTES = IDPaciente;

    }
}

Log.i("ID do Paciente", "IDS dos Pacientes: "+idPACIENTES);
//parametrosPost2.add(new BasicNameValuePair("idNome", idPACIENTES));

for(boolean respostas1 : respostas2){
    RESPOSTAS = respostas1;

}

Log.i("Resposta", "Respostas: "+RESPOSTAS);
//parametrosPost2.add(new BasicNameValuePair("respostas", String.valueOf(RESPOSTAS)));

I have 3 values that I would like to send to the database, so I understood, just this way I could send all... because there are several answers... If I put the answer inside the is, it will return me all of them so it will triple...

  • 1

    You could show how you fill in the values in your array respostas2? By the way, I don’t quite understand what your question is.

  • I’ve already edited the @Math

  • I believe that your problem of returning all false is occurring at the time of passing the values from one Activity to another, no?

  • Hard as it is, I already took the test. I put a Log inside the for and it returns me all right, but I needed to get the values out of the for, but then I’m having this problem of everything false

  • this answers is an array? pq if you are not assigning one on top of the other

  • ANSWERS is a Boolean...

  • if you take out of the comment the Log. i that is inside the is, and comment the Log. i that is outside, does not work?

  • You are using a for to take element to element of an array and put inside a boolean variable, the variable only holds a value, that is to each loop of the for you throw out the previous variable saved and assign a new one in the same place it overwrites the value.

  • 1

    It does, but I needed to get those values out of the for, if not the error... @Joannis, you’re right, he’s always getting the last answer... This assigning upon the other.. =(

  • need out of the for? put as you are reading that variable the part you are adding the parameters

  • Why out of the for? plays the go to where you assign

Show 6 more comments

1 answer

2


You’re wearing a for to take element the element of an array and put inside a boolean variable. The variable only holds one value, that is to each loop of the for you throw out the previous saved variable and assign a new one in the same place it overwrites the value.

Try this:

//------------------------------ENVIAR DADOS PARA O BANCO--------------------------

try{

    String urlPost2 = "http://"+l.IP+"/projetotcc/android/respostas.php";
    ArrayList<NameValuePair> parametrosPost2 = new ArrayList<NameValuePair>();

    String idPERGUNTAS = null;
    String idPACIENTES = null;
    boolean RESPOSTAS = false;
    //int intResposta = 0;

  //  
        for (final String a : ids){
            idPERGUNTAS = a;
            Log.i("ID da Perguntas", "IDS das Perguntas: "+idPERGUNTAS);
            parametrosPost2.add(new BasicNameValuePair("ids", idPERGUNTAS));
        }

        for (String IDPaciente : idPacientes){
            for (int i=0; i < respostas2.length; i++){
                idPACIENTES = IDPaciente;
                Log.i("ID do Paciente", "IDS dos Pacientes: "+idPACIENTES);
                parametrosPost2.add(new BasicNameValuePair("idNome", idPACIENTES));
            }
        }

        for(boolean respostas1 : respostas2){
             RESPOSTAS = respostas1;
             Log.i("ID da Resposta", "IDS das Respostas: "+RESPOSTAS);
             parametrosPost2.add(new BasicNameValuePair("respostas", String.valueOf(RESPOSTAS)));
        }

//-----------------------------------FIM - ENVIAR DADOS----------------------------------------
        for (int x=0; x<respostas2.length; x++){
        String respostaRetornada2 = null;

        try{
            respostaRetornada2 = ConexaoHttpClient.executaHttpPost(urlPost2, parametrosPost2);
            Log.i("Entrou", "Entrou");
            String resposta2 = respostaRetornada2.toString();
            resposta2 = resposta2.replaceAll("\\s+", "");

            Log.i("Logar", "Resposta: "+resposta2);

            if (resposta.equals("1")){
                Toast.makeText(getBaseContext(), "Respostas gravadas com sucesso!", Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(getBaseContext(), "Erro ao gravar respostas!", Toast.LENGTH_LONG).show();
            }

        }catch(Exception erro){
            f.mensagemSimples(Activity_Conf_Inicio_Ques.this, "Erro", "Erro: "+erro);
        }
    //}

}
  • http://meta.pt.stackoverflow.com/questions/2217/fechamento-de-uma-pergunta-indevida-ou-n%C3%A3o

Browser other questions tagged

You are not signed in. Login or sign up in order to post.