Reputation: 918
I've tried to get parent id of RadioButton (id of RadioGroup) by
RadioGroup rg = (RadioGroup) arg0.getParent();
but it failed. Following construction
RadioGroup rg = (RadioGroup) findViewById(R.id.radiogr);
works well. How can I get parent id at runtime?
fails with these logs:
12-26 17:11:05.205: E/AndroidRuntime(912): FATAL EXCEPTION: main
12-26 17:11:05.205: E/AndroidRuntime(912): java.lang.ClassCastException: android.widget.LinearLayout
12-26 17:11:05.205: E/AndroidRuntime(912): at com.letmedraw.shopcart.ShopCartActivity.onCheckedChanged(ShopCartActivity.java:96)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.widget.RadioGroup.access$600(RadioGroup.java:52)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:342)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.widget.CompoundButton.setChecked(CompoundButton.java:127)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.widget.CompoundButton.toggle(CompoundButton.java:86)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.widget.RadioButton.toggle(RadioButton.java:69)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.widget.CompoundButton.performClick(CompoundButton.java:98)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.view.View$PerformClick.run(View.java:8816)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.os.Handler.handleCallback(Handler.java:587)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.os.Handler.dispatchMessage(Handler.java:92)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.os.Looper.loop(Looper.java:123)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-26 17:11:05.205: E/AndroidRuntime(912): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 17:11:05.205: E/AndroidRuntime(912): at java.lang.reflect.Method.invoke(Method.java:521)
12-26 17:11:05.205: E/AndroidRuntime(912): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-26 17:11:05.205: E/AndroidRuntime(912): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-26 17:11:05.205: E/AndroidRuntime(912): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 3
Views: 12791
Reputation: 5261
Just to more clarify the accepted answer, You can get parent Id (in integer form) of view any view, Like this
int parentId = ((View) arg0.getParent()).getId();
And now you can compare id with R.id
to take your action. Because ids of all views are saved in R file
in Android in integer format.
if (parentId == R.id.your_id) { //
// Do whatever you want
}
Upvotes: 0
Reputation: 12346
All views (controls) in android are of type View
, so you can use getId()
to find the id.
int parentId = ((View) arg0.getParent()).getId();
Upvotes: 14
Reputation: 66637
It seems arg0 parent is LinearLayout, not Radio group and you are trying it to cast it as RadioGroup that is why it is throwing exception.
Upvotes: 0
Reputation: 23952
Because getParent() is not RadioGroup value, and not an id either. getParent() returns ViewParent type, not a View.
ViewParent is interface and View is a class.
Upvotes: 1