Natsu February 2016

Making checkbox checked in android with php and MySql

Hi guys I am trying to make my checkbox in my App to be checked when it receives the Data over php and mysql. I hope you will understand what I am going to ask, because my english is not so good. In my Images you can see my layout for the app and my database. I post you my php file and a part of my code. The Problem is, if I let it like this both checkboxes will be checked, because what they receive is true. My idea was that the checkbox part should be proof with String. E.g. (if result == "Male"){ setCheckboxMale(true); }... and so on. But it doesn't work this way because the result is boolean. What can I do? Pls help me. Thanks.

Layout Android

MySql Databse

get_data.php:

session_start();

if(isset($_GET['id'])){

$id = $_GET['id'];

$query = "SELECT st_id, st_name, st_surname, st_gender FROM T_Studnets WHERE  = $id";
}else{
$query = "SELECT st_id, st_name, st_surname, st_gender FROM T_Studnets"; } 

require_once("dbConnect.php");  

$result = mysqli_query($con,$query) or die('Error in query: $query' .mysql_error());

$output = array();

while($row = mysqli_fetch_assoc($result)){
    $output[] = $row;
}
print (json_encode($output));
mysqli_close($con);
?>

receive.java:

@Override
    protected void onPostExecute(Boolean result) {
        if (result == true) {

            etName.setText(data.getName());
            etSurname.setText(data.getSurname());
            etGender.setText(data.getGender());
            checkboxMale.setChecked(true);
            checkboxFemale.setChecked(true);

        } else {
            checkboxMale.setChecked(false);
            checkboxFemale.setChecked(false);
            Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
        }

        dialog.dismiss();
    }

Answers


TP89 February 2016

It would help if you showed us what data you receive in the Android code. If I'm reading it right you are recieving the gender as a String with either "Male" or "Female". If that's true then you could just do like you said yourself and write:

if(data.getGender().equals("Male"){
  checkboxMale.setChecked(true);
  checkboxFemale.setChecked(false);
}else{
  checkboxFemale.setChecked(true);
  checkboxMale.setChecked(false);
}

Post Status

Asked in February 2016
Viewed 3,947 times
Voted 4
Answered 1 times

Search




Leave an answer