Reputation: 15
This is my first post on Stackoverflow and so far it's been great finding a lot of information here, thank you for that!
I'm trying to have a list with selectable items. when you press the item it may prompt a dialog with 3 radiobuttons. I want to get the ID from the selected radiobutton but for now it always returns the same value. Since i made option 1(Klein) default. pressing a different button and getting that value won't help.
I'm looking forward to tips/tricks/help that you can provide me with(I recently started on android programming, so correct me where I'm wrong!)
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
if (groupPosition == 4) {
LayoutInflater li = LayoutInflater.from(context);
final View myView = li.inflate(R.layout.dialog, null);
final AlertDialog.Builder Builder = new AlertDialog.Builder(context);
// set layout
Builder.setView(dialog);
// set title
Builder.setTitle("Maak uw keuze");
// set dialog message
Builder
.setCancelable(true)
.setPositiveButton("Bestellen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
RadioGroup radioGroup1 = (RadioGroup)myView.findViewById(R.id.RadioGroup);
String radiovalue = ((RadioButton)myView.findViewById(radioGroup1.getCheckedRadioButtonId())).getText().toString();
// int selectId =radioGroup1.getCheckedRadioButtonId();
dialog.cancel();
Toast.makeText(MainActivity.this, radiovalue,
Toast.LENGTH_SHORT).show();
}
});
// create alert dialog
AlertDialog alertDialog = Builder.create();
// show it
alertDialog.show();
}
my xml(dialog.xml):
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RadioGroup"
android:orientation="horizontal"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checkedButton="@+id/radioButton1"
>
<RadioButton
android:text="Klein"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton1"
android:layout_alignBaseline="@+id/radioButton"
android:layout_alignBottom="@+id/radioButton"
android:layout_toRightOf="@+id/radioButton"
android:layout_toEndOf="@+id/radioButton"
/>
<RadioButton
android:text="Middel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton2"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<RadioButton
android:text="Groot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton3"
android:layout_alignBaseline="@+id/radioButton2"
android:layout_alignBottom="@+id/radioButton2"
android:layout_toRightOf="@+id/radioButton2"
android:layout_toEndOf="@+id/radioButton2"
android:layout_marginLeft="17dp"
android:layout_marginStart="17dp" />
</RadioGroup>
Upvotes: 1
Views: 356
Reputation: 15
I figured it out!
I had:
LayoutInflater li = LayoutInflater.from(context); final View myView = li.inflate(R.layout.dialog, null);
final AlertDialog.Builder Builder = new AlertDialog.Builder(context);
// set layout
Builder.setView(dialog);
This Build.setView should refer to my inflater... not the XML itself....
So Builder.setView(myView); Solved the issue!
Upvotes: 0
Reputation: 5550
UPDATED CODE:
.setPositiveButton("Bestellen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//keep this variables global outside onCreate just like ExpandableListView expListView;
RadioGroup radioGroup1; //this
RadioButton radioButton; // and this too keep global
radioGroup1 = (RadioGroup)myView.findViewById(R.id.RadioGroup);
int something = radioGroup1.getCheckedRadioButtonId();
radioButton = (RadioButton)myView.findViewById(something);
String radioValue = radioButton.getText().toString();
dialog.cancel();
Toast.makeText(MainActivity.this, radioValue,
Toast.LENGTH_SHORT).show();
}
});
Remove the attribute from XML:
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RadioGroup"
android:orientation="horizontal"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checkedButton="@+id/radioButton1" //removing this helped me so try to remove and apply the java code.
>
Upvotes: 1