これはStackoverflowに関する私の最初の投稿です。ここまで多くの情報を見つけることができました。ありがとうございました!ダイアログ内からCheckedRadioButtonIdを取得できません
私は選択可能なアイテムでリストを作成しようとしています。アイテムを押すと、3つのラジオボタンを持つダイアログが表示されることがあります。選択したラジオボタンからIDを取得したいのですが、現在は常に同じ値を返します。私はオプション1(Klein)をデフォルトにしています。別のボタンを押してその値を取得することは役に立ちません。
私はあなたが私を提供することができますヒント/トリック/ヘルプを楽しみにしています(私は最近、アンドロイドプログラミング上で起動するので、私が間違っているところ私を修正!)
// 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();
}
私のXMLは(ダイアログ.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>
Goodmorning、私はdialog.cancleを削除しようとしましたが、それでもアプリケーションがクラッシュする原因となるnull値を取得するので、役に立たないでしょう。あなたと画面を共有できる方法はありますか?私のコードを通過する? –
事はアンドロイドスタジオのコードを受け付けています。私は自分の携帯電話でそれを起動するときに選択します:Frites:Patat Frites 1,60、3つのラジオボタンのうちの1つを選択してください:bestellenをクリックして、null参照のためにクラッシュします... @ W4R10CK –
それは私の側、非常に明確な。 3つのラジオボタンを初期化して、 – W4R10CK