I can not store the correct Radio Button information in the database

0

I am not able to store the correct Radio Button information in the database. When I select the Public option it stores Public, when I select the Private option it stores Public. Can anyone help me?

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class CadastrarHospital extends ActionBarActivity{

    private EditText nome_hospital;
    private EditText cidade;
    private EditText estado;
    private EditText endereco;
    private Button cadastrar_hosp;
    private RadioButton publico;
    private RadioButton privado;
    private RadioGroup radioGroup;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_cadastrar_hospital);

        nome_hospital = (EditText)findViewById(R.id.nome_hospital);
        cidade = (EditText)findViewById(R.id.cidade);
        estado = (EditText)findViewById(R.id.estado);
        endereco = (EditText)findViewById(R.id.endereco);
        cadastrar_hosp = (Button)findViewById(R.id.cadastrar_hosp);
        publico = (RadioButton)findViewById(R.id.publico);
        privado = (RadioButton)findViewById(R.id.privado);
        radioGroup = (RadioGroup)findViewById(R.id.radioGroup);

    }
    public void insert(View view) {


        String nome = nome_hospital.getText().toString();
        String cidade_hospital = cidade.getText().toString();
        String estado_hospital = estado.getText().toString();
        String hospital_publico = publico.getText().toString();
        String hospital_privado = privado.getText().toString();

        insertToDatabase(nome, cidade_hospital, estado_hospital, hospital_publico, hospital_privado);
    }

    private void insertToDatabase(String nome, String cidade_hospital, String estado_hospital, String hospital_publico, String hospital_privado) {
        class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... params) {
                String paramUsername = params[0];
                String paramAddress = params[1];
                String paramCidade = params[2];


                String nome = nome_hospital.getText().toString();
                String cidade_hospital = cidade.getText().toString();
                String estado_hospital = estado.getText().toString();
                String hospital_publico = publico.getText().toString();
                String hospital_privado = privado.getText().toString();



                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("nome", nome));
                nameValuePairs.add(new BasicNameValuePair("cidade", cidade_hospital));
                nameValuePairs.add(new BasicNameValuePair("estado", estado_hospital));

                if (publico.getText().toString() == hospital_publico ) {
                    nameValuePairs.add(new BasicNameValuePair("tipo", hospital_publico));
                }else{
                    nameValuePairs.add(new BasicNameValuePair("tipo", hospital_privado));

                }



                try {
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(
                            "http://teste.org.br/aplicativo/inserir_hospitais.php");
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    HttpResponse response = httpClient.execute(httpPost);

                    HttpEntity entity = response.getEntity();


                } catch (ClientProtocolException e) {

                } catch (IOException e) {

                }
                return "success";
            }
            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);


                AlertDialog.Builder dialogo = new AlertDialog.Builder(CadastrarHospital.this);
                dialogo.setMessage("Cadastro realizado com sucesso!");
                dialogo.setPositiveButton("Fazer Denúncia", new DialogInterface.OnClickListener() {
                    // C DIGO QUE SER  EXECUTADO SE O USU RIO PRESSIONAR O BOT O N O - O usuario ser  levado para a dela de cadastro
                    public void onClick(DialogInterface dialog, int which) {
                        Intent itt = new Intent(CadastrarHospital.this, Denuncia.class);


                        startActivity(itt);
                    }
                });

                dialogo.show();

            }
        }
        SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
        sendPostReqAsyncTask.execute(nome,cidade_hospital,estado_hospital,hospital_publico,hospital_privado);
        }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_cadastrar_hospital, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}
    
asked by anonymous 15.09.2015 / 17:24

2 answers

1

Good afternoon! If you use it this way, I think your code will work.

Java

public class CadastrarHospital extends Activity {

private EditText nome_hospital;
private EditText cidade;
private EditText estado;
private EditText endereco;
private Button cadastrar_hosp;
private RadioGroup radioGroup;

private TextView statusSwitch;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_cadastrar_hospital);

    nome_hospital = (EditText)findViewById(R.id.nome_hospital);
    cidade = (EditText)findViewById(R.id.cidade);
    estado = (EditText)findViewById(R.id.estado);
    endereco = (EditText)findViewById(R.id.endereco);
    cadastrar_hosp = (Button)findViewById(R.id.cadastrar_hosp);
    radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
    statusSwitch = (TextView) findViewById(R.id.textView);


    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch(checkedId){
                case R.id.radioButton:
                    statusSwitch.setText("Público");
                    break;

                case R.id.radioButton2:
                    statusSwitch.setText("Privado");
                    break;



            }
        }
    });


}




public void insert(View view) {


    String nome = nome_hospital.getText().toString();
    String cidade_hospital = cidade.getText().toString();
    String estado_hospital = estado.getText().toString();
    String estatus = statusSwitch.getText().toString();



    insertToDatabase(nome, cidade_hospital, estado_hospital,estatus);


}



private void insertToDatabase(String nome, String cidade_hospital, String estado_hospital,String estatus) {
    class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {



        @Override
        protected String doInBackground(String... params) {
            String paramUsername = params[0];
            String paramAddress = params[1];
            String paramCidade = params[2];


            String nome = nome_hospital.getText().toString();
            String cidade_hospital = cidade.getText().toString();
            String estado_hospital = estado.getText().toString();
            String estatus = statusSwitch.getText().toString();





            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("nome", nome));
            nameValuePairs.add(new BasicNameValuePair("cidade", cidade_hospital));
            nameValuePairs.add(new BasicNameValuePair("estado", estado_hospital));
            nameValuePairs.add(new BasicNameValuePair("tipo",estatus));


            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(
                        "http://site.org.br/aplicativo/inserir_hospitais.php");
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = httpClient.execute(httpPost);

                HttpEntity entity = response.getEntity();


            } catch (ClientProtocolException e) {

            } catch (IOException e) {

            }
            return "success";
        }
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);


            AlertDialog.Builder dialogo = new AlertDialog.Builder(CadastrarHospital.this);
            dialogo.setMessage("Cadastro realizado com sucesso!");
            dialogo.setPositiveButton("Fazer Denúncia", new DialogInterface.OnClickListener() {
                // C DIGO QUE SER  EXECUTADO SE O USU RIO PRESSIONAR O BOT O N O - O usuario ser  levado para a dela de cadastro
                public void onClick(DialogInterface dialog, int which) {
                    Intent itt = new Intent(CadastrarHospital.this, Denuncia.class);


                    startActivity(itt);
                }
            });

            dialogo.show();

        }
    }
    SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
    sendPostReqAsyncTask.execute(nome,cidade_hospital,estado_hospital,estatus);
    }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_cadastrar_hospital, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

I have modified your radiogroup and also add another textview

 <RadioGroup
    android:layout_width="200dp"
    android:layout_height="82dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="0dp"
    android:layout_marginStart="45dp"
    android:layout_marginTop="0dp"
    android:id="@+id/radioGroup"
    >

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Publico"
        android:checked="true"
        android:id="@+id/radioButton" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Privado"
        android:id="@+id/radioButton2" />

</RadioGroup>



<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Público"
    android:id="@+id/textView"
    android:layout_below="@+id/switch1"
    android:layout_centerHorizontal="true"
    android:visibility="invisible"
    android:layout_marginTop="0dp" />

Just replace what I believe will work.

    
15.09.2015 / 18:32
0

Hello, try changing the following part of the code:

   if (publico.getText().toString() == hospital_publico ) {
          nameValuePairs.add(new BasicNameValuePair("tipo", hospital_publico));
      }else{
           nameValuePairs.add(new BasicNameValuePair("tipo", hospital_privado));
 }

TO:

 if (publico.getText().toString().equals(hospital_publico)) {
                    nameValuePairs.add(new BasicNameValuePair("tipo", hospital_publico));
                }else{
                    nameValuePairs.add(new BasicNameValuePair("tipo", hospital_privado));

                }

When we use == we compare the reference of the variable. So to compare the value of the variable we use equals !

As it is a radioButton, the correct form would be:

            if (publico.isChecked() ) {
                nameValuePairs.add(new BasicNameValuePair("tipo", hospital_publico));
            }else{
                nameValuePairs.add(new BasicNameValuePair("tipo", hospital_privado));

            }

I hope I have helped! Greetings.

    
15.09.2015 / 17:59