-1

Here in my program,i am creating a user profile fragment consisting of firstname, lastname , gender ,date of birth , address1, address2 and mobile number. I am getting null value in the gender field even though i made selection in the radio button.And i am getting the alert "please fill the gender field ". I am new in android programming.Help me to solve this. Here is my code.

package com.example.aparna.listfragment.fragments;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;


import com.example.aparna.listfragment.R;
import com.example.aparna.listfragment.database.DbCreate;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class ProfileFragment extends Fragment {


    public TextView tv_firstname,tv_lastname,tv_dob,tv_setdate,tv_address1,tv_address2,tv_mob;
    public EditText edittext_firstname,edittext_lastname,edittext_address1,edittext_address2,edittext_mob;
    public ImageView img_same,img_calender;
    public Button btn_save;
    public RadioGroup radioGroup;
    public RadioButton radiobtn_male,radiobtn_female,rb;
    public DbCreate create;
    public String firstname,lastname,saddress1,saddress2,mobile_no,gender,date_of_birth;
    public int mYear,mMonth,mDay;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_profile, container, false);
        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
        tv_firstname=(TextView)view.findViewById(R.id.tv_firstname);
        edittext_firstname=(EditText) view.findViewById(R.id.edittext_firstname);
        tv_lastname=(TextView)view.findViewById(R.id.tv_lastname);
        edittext_lastname=(EditText) view.findViewById(R.id.edittext_lastname);

        tv_address1=(TextView)view.findViewById(R.id.tv_address1);
        edittext_address1=(EditText)view.findViewById(R.id.edittext_address1);
        tv_address2=(TextView)view.findViewById(R.id.tv_address2);
        edittext_address2=(EditText)view.findViewById(R.id.edittext_address2);

        radioGroup=(RadioGroup)view.findViewById(R.id.radioGroup);
        btn_save=(Button)view.findViewById(R.id.btn_save);


        tv_dob=(TextView)view.findViewById(R.id.tv_dob);
        tv_setdate=(TextView) view.findViewById(R.id.tv_setdate);

        img_same=view.findViewById(R.id.img_same);
        img_calender=view.findViewById(R.id.img_calender);

        tv_dob=(TextView)view.findViewById(R.id.tv_mob);
        edittext_mob=(EditText) view.findViewById(R.id.edittext_mob);



        // date picker
        img_calender.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                final Calendar mcurrentDate=Calendar.getInstance();
                mYear=mcurrentDate.get(Calendar.YEAR);
                mMonth=mcurrentDate.get(Calendar.MONTH);
                mDay=mcurrentDate.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog mDatePicker=new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                        tv_setdate.setText(selectedday+"/"+selectedmonth+"/"+selectedyear);
                        date_of_birth=tv_setdate.getText().toString();
                        }
                },mYear,mMonth,mDay);
                mDatePicker.show();  }
        });



        //if address is same
        img_same.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edittext_address2.setText(edittext_address1.getText().toString());
                saddress2=saddress1;
                edittext_address2.setSelection(edittext_address2.getText().length());//moving cursor to the end of edit text

            }
        });






        // save button pressed
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                create=new DbCreate(getActivity());

                firstname=edittext_firstname.getText().toString();
                lastname=edittext_lastname.getText().toString();


                //radio button
                radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
                {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                       RadioButton rb= (RadioButton)group.findViewById(checkedId);
                        gender=rb.getText().toString();
                        Log.e("gender","=="+gender );
                    }
                });





                saddress1=edittext_address1.getText().toString();
                saddress2=edittext_address2.getText().toString();


                 mobile_no=edittext_mob.getText().toString();
                Log.e("mobile no ","=="+mobile_no);


//----------------------------------------------------------------------------------------------------

                if(firstname.isEmpty()) {
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the firstname field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                }

               else if(!firstname.matches("[a-zA-Z ]+") ) {
                    edittext_firstname.setError("ENTER ONLY ALPHABETICAL CHARACTER");
                    }


                    else if(lastname.isEmpty()){
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the lastname field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                  }

                else if( !lastname.matches("[a-zA-Z]+")) {
                    edittext_lastname.setError("ENTER ONLY ALPHABETICAL CHARACTER");
                }


                else if(gender.isEmpty()){
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the gender field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                }

                else if(date_of_birth.isEmpty() ){
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the dob field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                }

                else if( saddress1.isEmpty()){
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the address1 field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                }
                else if(saddress2.isEmpty()){
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the address2 field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                }

                else if(mobile_no.isEmpty()){
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the mobile no field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                }
                else if( mobile_no.length()!=10){
                    edittext_mob.setError("ENTER VALID NUMBER");

                }

                else{
                    create.insertUser(firstname,lastname,gender,date_of_birth,saddress1,saddress2,mobile_no);
                    //create.deleteAll();
                    Toast.makeText(getActivity(), "data saved", Toast.LENGTH_SHORT).show();
                    }

                Log.e("no. of","data=="+create.userCount() );
                }
        });

        return view;
    }

}
  • move your checkedchangedlistener outside of button click preferably in onCreate() – Vivek Mishra Sep 28 '18 at 04:20
  • Hi Please refer this page for asking good question which usually get upvoted,which helps in getting good answer [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and – v8-E Sep 28 '18 at 04:20

1 Answers1

0

Please Change this

   RadioButton rb= (RadioButton)group.findViewById(checkedId);

To this:

Paste this in OnCreatedView Remove from OnClick in your current code

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
            {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                   RadioButton rb= (RadioButton)view.findViewById(checkedId);
                    gender=rb.getText().toString();
                    Log.e("gender","=="+gender );
                }
            });

If you want it on Button Click follow this

Do findViewById in OnCreatedView

 radioSexGroup=(RadioGroup)findViewById(R.id.radioGroup);

Use this in Button Click:

        int selectedId=radioSexGroup.getCheckedRadioButtonId();
        radioSexButton=(RadioButton)findViewById(selectedId);

in Fragment :

  RadioButton rb= (RadioButton)view.findViewById(checkedId);
Faiz Mir
  • 599
  • 5
  • 16