-1

What method I have to use for two radio buttons as strings(eg: gender). String --the error "string declaration is not allowed here" I'm trying to add radio button to this code from website:Learn2crack

if(radioButton1.isSeleceted())
    String temp = radioButton1.getText().toString();
if(radioButton2.isSeleceted())
    String temp = radioButton2.getText().toString();

Source: Source of this if condition code


btn_register.setOnClickListener(this);
        tv_login.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_login:
                goToLogin();
                break;
            case R.id.btn_register:
                String name = et_name.getText().toString();
                String email = et_email.getText().toString();
                String password = et_password.getText().toString();
                String aadhar = et_aadhar.getText().toString();
                String phone = et_phone.getText().toString();
                String address = et_address.getText().toString();
                String gender = radioM.getText().toString();// i stucked here

My xml code:

 <RadioGroup
        android:id="@+id/radioGrp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp">
        <RadioButton
            android:id="@+id/radioM"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:checked="false"
            android:layout_weight="1"
            android:textSize="14dp"
            android:text="Male"
            />
        <RadioButton
            android:id="@+id/radioF"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:checked="false"
            android:layout_weight="1"
            android:textSize="14dp"
            android:text="Female"
            />
    </RadioGroup>

String declaration in java code for PHP MySql

private String gender;
 public String getGender() {
        return gender;
    }
 public void setGender(String gender) {
        this.gender = gender;// in my database text( eg: M/F) store in gender field
    }
Community
  • 1
  • 1
Arjun
  • 11
  • 6
  • `isSeleceted` is a typo. It's `isSelected`. You've got one too many `e`s. You probably don't want that method anyway. You want `isChecked()`. – Mike M. Mar 26 '17 at 09:05
  • yeah `isChecked()` is solved but `string declaration not allowed` error is not solved. @MikeM. – Arjun Mar 26 '17 at 09:11
  • Your question may have been solved . checkout this [enter link description here](http://stackoverflow.com/questions/11194515/android-get-value-of-the-selected-radio-button) – Shankar Acharya Mar 26 '17 at 09:15
  • Where is the database located? Are you storing data in a SQLite database on the Android device? Or are you storing the data in a database on some server on the internet? – Code-Apprentice Mar 26 '17 at 09:48
  • I'm using PHP MySQL database (Xampp server) @Code-Apprentice – Arjun Mar 26 '17 at 10:06

1 Answers1

0

Just get reference of Radio Group and radioGroup.getCheckedRadioButtonId() like below code you can getValue of radio button checkd

// get selected radio button from radioGroup
        int selectedId = radioGroup.getCheckedRadioButtonId();

        // find the radiobutton by returned id
        radioButton = (RadioButton) findViewById(selectedId);

    if(radioButton!=null)
    {
         Toast.makeText(MyAndroidAppActivity.this,
                    radioButton.getText(), Toast.LENGTH_SHORT).show();
    }
Nikhil Sharma
  • 593
  • 7
  • 23
  • I know this already I want to know how to declare it as a string (eg: gender as string ) – Arjun Mar 26 '17 at 09:32
  • @Arjun You did that already in the code in your question: `String gender`. – Code-Apprentice Mar 26 '17 at 09:47
  • for `btn_register` as i mentioned in my above code i have used switch case if i use this [http://stackoverflow.com/questions/11194515/android-get-value-of-the-selected-radio-button] link answer - it is in if condition but I want this gender for validation and as well as for register the user `(eg: user.setGender(gender)`. I hope you understand my doubt otherwise i'll send my full code to your email. @Code-Apprentice – Arjun Mar 26 '17 at 10:04