Ad

Retrieving The Selected Radio Button In An Alertdialog

- 1 answer

Goal:
Retrieve the selectedId of the checked radio button in alertdialog.

Problem:
You cannot retrieve the value by using findViewByid in method SpinnerDialogItemSelected because I get a error message that is:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RadioGroup.getCheckedRadioButtonId()' on a null object reference

How do you retrieve the value that can be used as a argument in method SpinnerDialogItemSelected?

Info:
*I'm new in Android
*I'm using android studio

Thank you!


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <Button
            android:id="@+id/alertFormElements2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="alertFormElements2"
            android:text="Button" />

</LinearLayout>

form_elements.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <RadioGroup
        android:id="@+id/rgtest1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp">

        <RadioButton
            android:id="@+id/test1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:checked="true"
            android:text="test1" />

        <RadioButton
            android:id="@+id/test2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="test1" />

    </RadioGroup>

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="131dp" />

</RelativeLayout>

MainActivity.java

package com.jfdimarzio.t1;

import android.annotation.TargetApi;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {

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


    private int _data = 0;


    /*
     * Show AlertDialog with some form elements.
     */
    public void alertFormElements2(View vdf) {

        dddd();
    }


    private void dddd()
    {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View formElementsView = inflater.inflate(R.layout.form_elements2,null, false);

        String[] vvalue = new String[]{
                "7",
                "3",
                "6",
                "3",
                "8",
                "9",
                "15",
                "11",
                "57"
        };


        final Spinner _spinner = formElementsView.findViewById(R.id.spinner);
        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.spinner_item, vvalue);
        spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
        _spinner.setAdapter(spinnerArrayAdapter);

        // the alert dialog
        new AlertDialog.Builder(MainActivity.this).setView(formElementsView)
                .setTitle("Form Elements")
                .setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int id) {
                        SpinnerDialogItemSelected(_spinner.getSelectedItem().toString());
                        dialog.dismiss();
                    }
                }).show();

    }


    private void SpinnerDialogItemSelected(String value)
    {
        RadioGroup myRadioGroup  = (RadioGroup) findViewById(R.id.rgtest1);
        int selectedId = myRadioGroup.getCheckedRadioButtonId();
    }


} // Class
Ad

Answer

RadioGroup myRadioGroup  = (RadioGroup) formElementsView.findViewById(R.id.rgtest1);
Ad
source: stackoverflow.com
Ad