0
I’m having trouble storing the correct information of a radio button 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);
}
}
Now It Worked! Valew!
– Thayna Rocha
If you can explain why your code solves the problem, both the author of the question and whoever finds the same problem will learn something new ;) This also increases the chances of +1
– brasofilo
What I did, I created a textview that receives the value passed by the radios inside my case, then asked to insert what was returned from textview, then left it invisible android:visibility="Invisible" not to appear on the screen.
– Henrique S. Santos