Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create RadioButton Dynamically with String Array

Tags:

android

i m trying to create RadioButton from String Array but it is not working if i use only String then it seems to work fine Log Cat Showing error "ava.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first."

i know that error is coming beacuse RadioButton Object is not Creating Dynamically, so that error is showing

i took the Code From here for Reference,Please check this url http://android.okhelp.cz/create-radiobutton-radiogroup-dynamically-android-sample/

Please check code and correct me so i will go further with my App Here is code

LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
RadioGroup radioGroup = new RadioGroup(this);

LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );

        // adding Radio Group
        layout.addView(radioGroup, p);

        // creating Radio buttons Object//
        RadioButton radioButtonView = new RadioButton(this);
        String[] ab ={"1","2"}; 

        for(int i =0; i<ab.length;i++)
        {
            radioButtonView.setText(ab[i]);
            radioGroup.addView(radioButtonView, p);
            ((ViewGroup)layout.getParent()).removeView(layout);
        }

and Log Cat Error

 I/Choreographer(4431): Skipped 547 frames!  The application may be doing too much work on its main thread.
 D/AndroidRuntime(4431): Shutting down VM
 W/dalvikvm(4431): threadid=1: thread exiting with uncaught exception (group=0xb2d2fb20)
  FATAL EXCEPTION: main
  Process: com.example.radiobuttondynamic, PID: 4431
  java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
    at android.view.ViewGroup.addView(ViewGroup.java:3415)
    at android.widget.RadioGroup.addView(RadioGroup.java:141)
    at android.view.ViewGroup.addView(ViewGroup.java:3391)
    at com.example.radiobuttondynamic.Radio_Button.onCreateOptionsMenu(Radio_Button.java:46)
    at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
    at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
    at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
    at android.view.Choreographer.doFrame(Choreographer.java:543)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
 D/dalvikvm(4431): GC_FOR_ALLOC freed 142K, 7% free 3094K/3308K, paused 107ms, total 113ms

Looking for help thanks

like image 930
Ashu Kumar Avatar asked Feb 11 '23 05:02

Ashu Kumar


1 Answers

If you have this array:-

String route[] = {"1", "3", "4"};

You can create radio buttons using the loop:-

for (int i = 0; i < route.length; i++) 
{
    RadioButton radioButton = new RadioButton(this);
    radioButton.setText("Route " + String.valueOf(route[i]));
    radioButton.setId(i);
    rprms = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
    rgp.addView(radioButton, rprms);
}

And you can get the selected field using:- (I am having SAVE button, so this code is written in onClick listener) Else you can use onCheckedChangeListener also.

int selection = rgp.getCheckedRadioButtonId();
for (int i = 0; i < route.length; i++) 
{
    if (i == selection)
        showToast("" + route[i]);
}
like image 76
DsD Avatar answered Feb 15 '23 09:02

DsD